public IHttpActionResult DoAutoLogin([FromBody] JToken json)
 {
     return(this.TryReturn <object>(() =>
     {
         try
         {
             var jsn = json.AsDynamic();
             string jsCode = jsn.jsCode;
             //根据JsCode换取OpenId
             var jsCode2Session = WxPayData.GetOpenId(jsCode);
             var openId = jsCode2Session.openid;
             if (openId == "")
             {
                 return new { Table = "", IS_SUCCESS = false, MSG = "JsCode失效,未获取到OpenId" };
             }
             else
             {
                 using (var x = Join.Dal.MySqlProvider.X())
                 {
                     //根据OPEN_ID去数据库查询登录信息
                     var searchSql = "select * from a_user where open_id='" + openId + "'";
                     var openIdDt = x.ExecuteSqlCommand(searchSql);
                     x.Close();
                     //如果根据OPEN_ID查询不到,不能自动登录
                     if (openIdDt.Tables[0].Rows.Count > 0)
                     {
                         return new { Table = openIdDt.Tables[0], IS_SUCCESS = true, MSG = "" };
                     }
                     else
                     {
                         return new { Table = "", IS_SUCCESS = false, MSG = "请先登录/注册" };
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             return new { Table = "", IS_SUCCESS = false, MSG = ex.Message };
         }
     }));
 }
 public IHttpActionResult DoLogin([FromBody] JToken json)
 {
     return(this.TryReturn <object>(() =>
     {
         try
         {
             var jsn = json.AsDynamic();
             string jsCode = jsn.jsCode;
             string baseName = jsn.name;
             var baseStr = Convert.FromBase64String(baseName);
             var name = Encoding.UTF8.GetString(baseStr);
             string url = jsn.url;
             string encryptedData = jsn.encryptedData;
             string iv = jsn.iv;
             //根据JsCode换取OpenId
             var jsCode2Session = WxPayData.GetOpenId(jsCode);
             var openId = jsCode2Session.openid;
             if (openId == "" || openId == null)
             {
                 return new { Table = "", IS_SUCCESS = false, MSG = "JsCode失效,未获取到OpenId" };
             }
             else
             {
                 //解密手机号
                 var wxModel = DescodeWxSHA1(encryptedData, jsCode2Session.session_key, iv);
                 var tel = wxModel.PhoneNumber;
                 using (var x = Join.Dal.MySqlProvider.X())
                 {
                     //根据手机号去数据库查询登录信息
                     var searchSql = "select * from a_user where is_delete=0 and PHONE='" + tel + "'";
                     var dt = x.ExecuteSqlCommand(searchSql);
                     //如果查询到数据,更新OPEN_ID
                     if (dt.Tables[0].Rows.Count > 0)
                     {
                         var kid = dt.Tables[0].Rows[0]["KID"].ToString();
                         //执行插入OPEN_ID
                         var updateSql = string.Format(@"update a_user set open_id='{0}' where kid='{1}'", openId, kid);
                         x.ExecuteSqlCommand(updateSql);
                     }
                     //如果根据手机号查询不到,执行创建用户
                     else
                     {
                         //手机号后4位
                         var password = tel.Substring(tel.Length - 4, 4);
                         var insertSql = string.Format(@"insert into a_user (`CODE`,`NAME`,`PHONE`,`OPEN_ID`,`PASSWORD`,`IMG_URL`,`REGIST_DATE`,`IS_DELETE`,`IS_SA`) 
                                                     values('{0}','{1}','{2}','{3}','{4}','{5}','{6}',0,0)"
                                                       , DateTime.Now.ToString("yyyyMMddHHmmss"), name, tel, openId,
                                                       password, url, DateTime.Now.ToString("yyyy-MM-dd"));
                         x.ExecuteSqlCommand(insertSql);
                     }
                     //根据手机号去数据库查询登录信息
                     var searchResultSql = "select * from a_user where is_delete=0 and PHONE='" + tel + "'";
                     var sdt = x.ExecuteSqlCommand(searchResultSql);
                     x.Close();
                     return new { Table = sdt.Tables[0], IS_SUCCESS = true, MSG = "" };
                 }
             }
         }
         catch (Exception ex)
         {
             return new { Table = "", IS_SUCCESS = false, MSG = ex.Message };
         }
     }));
 }
        public IHttpActionResult WeChatServicesPayApi([FromBody] JToken json)
        {
            return(this.TryReturn <object>(() =>
            {
                try
                {
                    WxPayData wxp = new WxPayData();
                    wxp.WriteLogFile("调用支付Json:" + json.ToJsonString());
                    var arg = json.AsDynamic();
                    var ip = GetClientIpAddress();
                    //金额
                    string price = arg.price;
                    //订单号
                    string orderNo = arg.orderNo;
                    //产品ID
                    string productId = arg.productId;
                    //买家ID
                    string buyUserId = arg.buyUserId;
                    //JsCode
                    string jsCode = arg.jsCode;
                    //获取OpenId
                    var openId = WxPayData.GetOpenId(jsCode).openid;
                    if (openId == "" || openId == null)
                    {
                        var msg = "JSCODE " + jsCode + "获取不到openId";
                        wxp.WriteLogFile(msg);
                        return new { Table = new { MSG = "", IsSuccess = false, ErroMessage = msg } };
                    }
                    #region 锁定产品状态
                    //执行sql
                    using (var x = Join.Dal.MySqlProvider.X())
                    {
                        //获取产品ID 查询产品是否在上架时间并且状态为上架中
                        var nowDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        var selectProductSql = "select * from B_PRODUCT_LIST where status='上架中' and kid='" + productId + "' and OFF_SHELF_TIME > '" + nowDate + "'";
                        var selectProductTables = x.ExecuteSqlCommand(selectProductSql);
                        if (selectProductTables.Tables[0].Rows.Count > 0)
                        {
                            //执行调用付款
                            //构造附加数据
                            string attach = productId + "|" + buyUserId;
                            var url = GlobalVariableWeChatApplets.UNIFIEDORDER_URL;
                            var data = WxPayData.ForApplets(double.Parse(price), openId, orderNo, ip, attach);
                            var xml = data.ToXml();
                            var response = HttpService.Post(xml, url, 6);
                            var preOrder = WxPayData.FromXml(response, "HMAC-SHA256");
                            var errCode = preOrder.GetValue("err_code");
                            if (errCode != null)
                            {
                                var errMsg = preOrder.GetValue("err_code_des");
                                return new { Table = new { MSG = "", IsSuccess = false, ErroMessage = errMsg } };
                            }
                            else
                            {
                                //如果调起支付成功,锁定产品状态为已锁定
                                var updateProductSql = "update b_product_list set status='已锁定',LOCK_TIME='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where kid = '" + productId + "'";
                                x.ExecuteSqlCommand(updateProductSql);

                                var payData = WxPayData.ForWechatPay(preOrder);
                                var orderString = payData.ToJson();
                                return new { Table = new { MSG = orderString, IsSuccess = true, ErroMessage = string.Empty } };
                            }
                        }
                        else
                        {
                            return new { Table = new { MSG = "", IsSuccess = false, ErroMessage = "产品已下架。" } };
                        }
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    return new { Table = new { MSG = "", IsSuccess = false, ErroMessage = ex.Message } };
                }
            }));
        }