UserService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\common\service;
  3. use GuzzleHttp\Client;
  4. use think\Db;
  5. use think\Exception;
  6. class UserService
  7. {
  8. private $model = null;
  9. /**
  10. * 初始化方法
  11. */
  12. public function __construct()
  13. {
  14. $this->model = Db::name('user');
  15. }
  16. /**
  17. * 用户换绑
  18. * @return void
  19. */
  20. public function userBindCompany($params=[])
  21. {
  22. $result = [
  23. 'status' => 1,
  24. 'msg' => '操作成功',
  25. 'data' => [],
  26. ];
  27. try {
  28. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  29. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  30. $where['id'] = $userId;
  31. $user = Db::name('user')->where($where['id'])->find();
  32. if (!empty($user)) {
  33. if ($user['company_id'] != $companyId) {
  34. $userData['company_id'] = $companyId;
  35. $userData['updatetime'] = time();
  36. $userRes = Db::name('user')->where($where['id'])->update($userData);
  37. if (!$userRes) {
  38. throw new Exception('操作失败');
  39. }
  40. }
  41. }
  42. } catch (Exception $e) {
  43. $result['status'] = 0;
  44. $result['msg'] = $e->getMessage();
  45. }
  46. return $result;
  47. }
  48. /**
  49. * 用户绑定门店和钱包
  50. * @return void
  51. */
  52. public function userWallet($params=[])
  53. {
  54. $result = [
  55. 'status' => 1,
  56. 'msg' => '操作成功',
  57. 'data' => [],
  58. ];
  59. try {
  60. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  61. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  62. $comefrom = isset($params['comefrom']) ? $params['comefrom'] : '';
  63. $bindRes = $this->userBindCompany($params);//绑定门店
  64. if (!$bindRes['status']) {
  65. throw new Exception($bindRes['msg']);
  66. }
  67. $where['user_id'] = $userId;
  68. $where['company_id'] = $companyId;
  69. $userWallet = Db::name('user_wallet')->where($where['id'])->find();
  70. $time = time();
  71. if (empty($userWallet)) {
  72. $userWalletData = [
  73. 'user_id' => $userId,
  74. 'company_id' => $companyId,
  75. 'money' => 0.00,
  76. 'createtime' => $time,
  77. 'comefrom' => $comefrom,
  78. ];
  79. $userWalletRes = Db::name('user_wallet')->insertGetId($userWalletData);
  80. if (!$userWalletRes) {
  81. throw new Exception('生成钱包失败');
  82. }
  83. }
  84. } catch (Exception $e) {
  85. $result['status'] = 0;
  86. $result['msg'] = $e->getMessage();
  87. }
  88. return $result;
  89. }
  90. /**
  91. * 微信消息发送
  92. * @return void
  93. */
  94. public function wechatMessageSend($params=[])
  95. {
  96. $result = [
  97. 'status' => 1,
  98. 'msg' => '操作成功',
  99. 'data' => [],
  100. ];
  101. try {
  102. $preOrderId = isset($params['pre_order_id']) ? $params['pre_order_id'] : 0;
  103. $p = 'pre_order';
  104. $u = 'user';
  105. $c = 'company';
  106. $s = 'servicetype';
  107. $field = $p.'.*,'.$u.'.mini_openid,'.$c.'.name as `company_name`,'.$s.'.title as `service_title`';
  108. $where[$p.'.id'] = $preOrderId;
  109. $preOrder = Db::name($p)->alias($p)->field($field)
  110. ->join($u,$u.'.id = '.$p.'.user_id','LEFT')
  111. ->join($c,$c.'.id = '.$p.'.company_id','LEFT')
  112. ->join($s,$s.'.id = '.$p.'.servicetype_id','LEFT')
  113. ->where($where)->find();
  114. if (empty($preOrder)) {
  115. throw new Exception('未找到预约信息');
  116. }
  117. $client = new Client();
  118. $tk = getAccessToken();
  119. /*预约日期 {{time1.DATA}}
  120. 场地{{thing8.DATA}}
  121. 预约类型{{thing7.DATA}}
  122. 顾客称号{{thing3.DATA}}*/
  123. $uri = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$tk;
  124. $openId = $preOrder['mini_openid'];
  125. $wechatTemplate = config('param.wechat_template');
  126. $preOrderMsg = isset($wechatTemplate['pre_order']) ? $wechatTemplate['pre_order'] : [];
  127. $data = [//数据内容
  128. "time1" => ["value" => date('Y年m月d日 H:i:s',$preOrder['pre_time'])],
  129. "thing8" => ["value" => $preOrder['company_name']],
  130. "thing7" => ["value" => $preOrder['service_title']],
  131. "thing3" => ["value" => $preOrder['name']],
  132. ];
  133. $jsonData = [
  134. "touser" => $openId, //openid
  135. "template_id" => $preOrderMsg['template_id'], //模版ID
  136. "page" => $preOrderMsg['page'], //跳转地址
  137. "miniprogram_state" => $preOrderMsg['miniprogram_state'],//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
  138. "lang" => $preOrderMsg['lang'], //支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
  139. "data" => $data, //数据
  140. ];
  141. $res = $client->request('POST', $uri, ['json' => $jsonData]);
  142. $returnResJson = $res->getBody()->getContents();
  143. $returnRes = json_decode($returnResJson, true);
  144. if ($returnRes['errcode'] != 0) {
  145. throw new Exception($returnRes['errmsg']);
  146. }
  147. } catch (Exception $e) {
  148. $result['status'] = 0;
  149. $result['msg'] = $e->getMessage();
  150. }
  151. return $result;
  152. }
  153. }