User.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace app\common\model;
  3. use app\common\model\Order;
  4. use app\common\model\Sms;
  5. use Firebase\JWT\JWT;
  6. use think\Model;
  7. use app\common\library\Common;
  8. use think\facade\Validate;
  9. use think\Request;
  10. use think\Db;
  11. use app\common\library\WxService;
  12. use app\common\library\WxPay;
  13. use app\common\library\AliPay;
  14. use EasyWeChat\Factory;
  15. header('Access-Control-Allow-Origin: *');
  16. /**
  17. * 会员模型
  18. */
  19. class User Extends Model
  20. {
  21. // 开启自动写入时间戳字段
  22. protected $autoWriteTimestamp = 'TIMESTAMP';
  23. // 定义时间戳字段名
  24. protected $createTime = 'create_at';
  25. protected $updateTime = 'update_at';
  26. // 追加属性
  27. protected $append = [
  28. ];
  29. protected $table = 'q_user';
  30. /**
  31. * 微信授权登录、注册
  32. */
  33. public static function wechatLogin($code,$rawData){
  34. $appid = Config::get_values('wechat_appid');
  35. $secret = Config::get_values('wechat_appsecret');
  36. $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
  37. $session_key = Common::curlRequest($url);
  38. if (!empty($session_key['session_key'])) {
  39. $openid = $session_key['openid'];
  40. //数据库是否已注册
  41. $user = Db::name('q_member')
  42. ->where('openid',$openid)
  43. ->where('is_deleted',1)
  44. ->find();
  45. if ($user){
  46. $token = JWT::encode($user,config('jwt.key'));
  47. $result['token'] = $token;
  48. return Common::return_success('授权成功',$result);
  49. }else{
  50. $data['sex'] = 0;
  51. if ($rawData){
  52. $data['rawdata'] = $rawData;
  53. $rawData = json_decode($rawData,true);
  54. $data['nickname'] = $rawData['nickName'];
  55. $data['openid'] = $openid;
  56. $data['sex'] = $rawData['gender'];
  57. $data['headimg'] = $rawData['avatarUrl'];
  58. }
  59. Db::startTrans();
  60. try {
  61. $user = self::create($data);
  62. Db::commit();
  63. $userId =$user->id;
  64. $use = Db::name('q_user')->where('id',$userId)->find();
  65. $token = JWT::encode($use,config('jwt.key'));
  66. $result['token'] = $token;
  67. return Common::return_success('授权成功',$result);
  68. }catch (Exception $e) {
  69. Db::rollback();
  70. return Common::return_error('授权失败');
  71. }
  72. }
  73. }else{
  74. return Common::return_error('获取session_key失败!');
  75. }
  76. }
  77. /**
  78. * 根据手机号获取用户信息
  79. */
  80. public static function getByMobile($phone){
  81. $userinfo = self::where('phone',$phone)->find();
  82. return $userinfo;
  83. }
  84. /**
  85. * 获取用户信息
  86. */
  87. public static function getUserInfo($id){
  88. $info = self::where('id',$id)->find();
  89. return $info;
  90. }
  91. /**
  92. * 修改密码
  93. */
  94. public static function editPayPassword($user_id,$ver_code,$password){
  95. $phone_use = self::where('id',$user_id)->find();
  96. //获取最后的验证码
  97. $time = time()-90;
  98. $sms = Sms::where(['mobile' => $phone_use['phone'], 'event' => 'changepwd'])
  99. ->where('createtime','>',$time)
  100. ->order('id', 'DESC')
  101. ->find();
  102. if (!$sms || $sms->code != $ver_code){
  103. return Common::return_error('短信验证码不正确!');
  104. }
  105. $phone_use->pay_password = md5($password);
  106. Db::startTrans();
  107. try {
  108. $phone_use->save();
  109. Db::commit();
  110. return Common::return_success('修改成功');
  111. }catch (Exception $e) {
  112. Db::rollback();
  113. return Common::return_error('修改失败');
  114. }
  115. }
  116. /**
  117. * 编辑用户资料
  118. */
  119. public static function EditUserInfo($user_id,$data){
  120. if (self::where('id',$user_id)->update($data)){
  121. return Common::return_success('编辑成功');
  122. }else{
  123. return Common::return_error('编辑失败');
  124. }
  125. }
  126. /**
  127. * 我的送检人列表
  128. */
  129. public static function peoplelist($user_id){
  130. $list = MakesPeople::where('user_id',$user_id)
  131. ->order('id asc')
  132. ->select();
  133. return Common::return_success('成功',$list);
  134. }
  135. /**
  136. * 添加,修改送检人
  137. */
  138. public static function AddEditPeoplelist($user_id,$data,$id){
  139. if ($id){
  140. $address = MakesPeople::where('id',$id)->where('user_id',$user_id)->find();
  141. if (!$address) return Common::return_error('信息不存在');
  142. //编辑
  143. if ($address->save($data)){
  144. return Common::return_success('编辑成功');
  145. }else{
  146. return Common::return_error('编辑失败');
  147. }
  148. }else{
  149. $data['user_id'] = $user_id;
  150. $addre = MakesPeople::create($data);
  151. if ($addre){
  152. return Common::return_success('添加成功');
  153. }else{
  154. return Common::return_error('添加失败');
  155. }
  156. }
  157. }
  158. /**
  159. * 删除送检人
  160. */
  161. public static function DelPeoplelist($user_id,$id){
  162. $address = MakesPeople::where('id',$id)->where('user_id',$user_id)->find();
  163. if (!$address) return Common::return_error('信息不存在');
  164. if (MakesPeople::where('id',$id)->delete()){
  165. return Common::return_success('删除成功');
  166. }else{
  167. return Common::return_error('删除失败');
  168. }
  169. }
  170. /**
  171. * 变更会员余额
  172. * @param int $money 余额
  173. * @param int $user_id 会员ID
  174. * @param string $memo 备注
  175. */
  176. public static function money($money, $user_id, $memo, $type,$pm = 0,$phone='',$fee=0,$status=0)
  177. {
  178. $user = self::get($user_id);
  179. if ($user)
  180. {
  181. switch ($type){
  182. case 1:
  183. $user_money = $user->equity_integral;
  184. $field = 'equity_integral';
  185. break;
  186. case 2:
  187. $user_money = $user->usable_integral;
  188. $field = 'usable_integral';
  189. break;
  190. case 3:
  191. $user_money = $user->repertory_integral;
  192. $field = 'repertory_integral';
  193. break;
  194. case 4:
  195. $user_money = $user->coupon;
  196. $field = 'coupon';
  197. break;
  198. }
  199. $before = $user_money;
  200. if ($pm==1){
  201. $after = $user_money + $money;
  202. }else{
  203. $after = $user_money - $money;
  204. }
  205. //更新会员信息
  206. $user->save([$field => $after]);
  207. //写入日志
  208. MoneyLog::create(['user_id' => $user_id,'pm' => $pm, 'change_money' => $money, 'before' => $before, 'after' => $after, 'title' => $memo,'type'=>$type,'phone'=>$phone,'fee'=>$fee,'status'=>$status]);
  209. }
  210. }
  211. }