UserService.php 7.0 KB

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