User.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 = 'store_member';
  30. /**
  31. * 微信授权登录、注册
  32. */
  33. public static function wechatLogin($code,$rawData){
  34. $appid = 'wx36e94e688b2db929';
  35. $secret = '475d867175d57f50fc70845b9d67548d';
  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('store_member')
  42. ->where('openid',$openid)
  43. ->find();
  44. if ($user){
  45. $token = JWT::encode($user,config('jwt.key'));
  46. $result['token'] = $token;
  47. $result['vip_level'] = $user['vip_level'];
  48. return Common::return_success('授权成功',$result);
  49. }else{
  50. if ($rawData){
  51. $data['rawdata'] = $rawData;
  52. $rawData = json_decode($rawData,true);
  53. $data['nickname'] = $rawData['nickName'];
  54. $data['openid'] = $openid;
  55. $data['sex'] = 1;
  56. $data['headimg'] = $rawData['avatarUrl'];
  57. $data['vip_number']=rand(100,999).rand(100,999).rand(1,9);
  58. $data['vip_level'] = 4;
  59. }
  60. Db::startTrans();
  61. try {
  62. $user = self::create($data);
  63. Db::commit();
  64. $userId =$user->id;
  65. $use = Db::name('store_member')->where('id',$userId)->find();
  66. $url = 'https://xiangqinim.zhousi.hdlkeji.com/addons/fastim/api.user/createUser?username='.$use['nickname'].'&avatar='.$use['headimg'].'&id='.$userId;
  67. $im_id = curlRequest($url);
  68. Db::name('store_member')->where('id',$userId)->update(['im_id'=>$im_id]);
  69. $token = JWT::encode($use,config('jwt.key'));
  70. $result['token'] = $token;
  71. $result['vip_level'] = $user['vip_level'];
  72. return Common::return_success('授权成功',$result);
  73. }catch (Exception $e) {
  74. Db::rollback();
  75. return Common::return_error('授权失败');
  76. }
  77. }
  78. }else{
  79. return Common::return_error('获取session_key失败!');
  80. }
  81. }
  82. /**
  83. * 微信授权登录、注册
  84. */
  85. public static function PhoneLogin($phone){
  86. $user = Db::name('store_member')
  87. ->where('phone',$phone)
  88. ->find();
  89. if ($user){
  90. $token = JWT::encode($user,config('jwt.key'));
  91. $result['token'] = $token;
  92. $result['vip_level'] = $user['vip_level'];
  93. return Common::return_success('授权成功',$result);
  94. }else{
  95. $data['phone'] = $phone;
  96. $data['sex'] = 1;
  97. $data['headimg'] = common_url().'/main/start.png';
  98. $data['vip_number']=rand(100,999).rand(100,999).rand(1,9);
  99. $data['vip_level'] = 4;
  100. Db::startTrans();
  101. try {
  102. $user = self::create($data);
  103. Db::commit();
  104. $userId =$user->id;
  105. $use = Db::name('store_member')->where('id',$userId)->find();
  106. $url = 'https://xiangqinim.zhousi.hdlkeji.com/addons/fastim/api.user/createUser?username='.$use['nickname'].'&avatar='.$use['headimg'].'&id='.$userId;
  107. $im_id = curlRequest($url);
  108. Db::name('store_member')->where('id',$userId)->update(['im_id'=>$im_id]);
  109. $token = JWT::encode($use,config('jwt.key'));
  110. $result['token'] = $token;
  111. $result['vip_level'] = $user['vip_level'];
  112. return Common::return_success('授权成功',$result);
  113. }catch (Exception $e) {
  114. Db::rollback();
  115. return Common::return_error('授权失败');
  116. }
  117. }
  118. }
  119. /**
  120. * 根据手机号获取用户信息
  121. */
  122. public static function getByMobile($phone){
  123. $userinfo = self::where('phone',$phone)->find();
  124. return $userinfo;
  125. }
  126. /**
  127. * 获取用户信息
  128. */
  129. public static function getUserInfo($id){
  130. $info = self::where('id',$id)->find();
  131. return $info;
  132. }
  133. /**
  134. * 修改密码
  135. */
  136. public static function editPayPassword($user_id,$ver_code,$password){
  137. $phone_use = self::where('id',$user_id)->find();
  138. //获取最后的验证码
  139. $time = time()-90;
  140. $sms = Sms::where(['mobile' => $phone_use['phone'], 'event' => 'changepwd'])
  141. ->where('createtime','>',$time)
  142. ->order('id', 'DESC')
  143. ->find();
  144. if (!$sms || $sms->code != $ver_code){
  145. return Common::return_error('短信验证码不正确!');
  146. }
  147. $phone_use->pay_password = md5($password);
  148. Db::startTrans();
  149. try {
  150. $phone_use->save();
  151. Db::commit();
  152. return Common::return_success('修改成功');
  153. }catch (Exception $e) {
  154. Db::rollback();
  155. return Common::return_error('修改失败');
  156. }
  157. }
  158. /**
  159. * 编辑用户资料
  160. */
  161. public static function EditUserInfo($user_id,$data){
  162. if (self::where('id',$user_id)->update($data)){
  163. return Common::return_success('编辑成功');
  164. }else{
  165. return Common::return_error('编辑失败');
  166. }
  167. }
  168. /**
  169. * 我的送检人列表
  170. */
  171. public static function peoplelist($user_id){
  172. $list = MakesPeople::where('user_id',$user_id)
  173. ->order('id asc')
  174. ->select();
  175. return Common::return_success('成功',$list);
  176. }
  177. /**
  178. * 添加,修改送检人
  179. */
  180. public static function AddEditPeoplelist($user_id,$data,$id){
  181. if ($id){
  182. $address = MakesPeople::where('id',$id)->where('user_id',$user_id)->find();
  183. if (!$address) return Common::return_error('信息不存在');
  184. //编辑
  185. if ($address->save($data)){
  186. return Common::return_success('编辑成功');
  187. }else{
  188. return Common::return_error('编辑失败');
  189. }
  190. }else{
  191. $data['user_id'] = $user_id;
  192. $addre = MakesPeople::create($data);
  193. if ($addre){
  194. return Common::return_success('添加成功');
  195. }else{
  196. return Common::return_error('添加失败');
  197. }
  198. }
  199. }
  200. /**
  201. * 删除送检人
  202. */
  203. public static function DelPeoplelist($user_id,$id){
  204. $address = MakesPeople::where('id',$id)->where('user_id',$user_id)->find();
  205. if (!$address) return Common::return_error('信息不存在');
  206. if (MakesPeople::where('id',$id)->delete()){
  207. return Common::return_success('删除成功');
  208. }else{
  209. return Common::return_error('删除失败');
  210. }
  211. }
  212. /**
  213. * 变更会员余额
  214. * @param int $money 余额
  215. * @param int $user_id 会员ID
  216. * @param string $memo 备注
  217. */
  218. public static function money($money, $user_id, $memo, $type,$pm = 0,$phone='',$fee=0,$status=0)
  219. {
  220. $user = self::get($user_id);
  221. if ($user)
  222. {
  223. switch ($type){
  224. case 1:
  225. $user_money = $user->equity_integral;
  226. $field = 'equity_integral';
  227. break;
  228. case 2:
  229. $user_money = $user->usable_integral;
  230. $field = 'usable_integral';
  231. break;
  232. case 3:
  233. $user_money = $user->repertory_integral;
  234. $field = 'repertory_integral';
  235. break;
  236. case 4:
  237. $user_money = $user->coupon;
  238. $field = 'coupon';
  239. break;
  240. }
  241. $before = $user_money;
  242. if ($pm==1){
  243. $after = $user_money + $money;
  244. }else{
  245. $after = $user_money - $money;
  246. }
  247. //更新会员信息
  248. $user->save([$field => $after]);
  249. //写入日志
  250. 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]);
  251. }
  252. }
  253. }