123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <?php
- namespace app\common\service;
- use app\common\model\PreOrder;
- use fast\Random;
- use GuzzleHttp\Client;
- use think\Db;
- use think\Exception;
- use think\Log;
- class UserService
- {
- private $model = null;
- /**
- * 初始化方法
- */
- public function __construct()
- {
- $this->model = Db::name('user');
- }
- /**
- * 用户换绑
- * @return void
- */
- public function userBindCompany($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '操作成功',
- 'data' => [],
- ];
- try {
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
- $where['id'] = $userId;
- $user = Db::name('user')->where($where['id'])->find();
- if (!empty($user)) {
- if ($user['company_id'] != $companyId) {
- $userData['company_id'] = $companyId;
- $userData['updatetime'] = time();
- $userRes = Db::name('user')->where($where)->update($userData);
- if (!$userRes) {
- throw new Exception('操作失败');
- }
- }
- }
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- }
- return $result;
- }
- /**
- * 用户绑定门店和钱包
- * @return void
- */
- public function userWallet($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '操作成功',
- 'data' => [],
- ];
- try {
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
- $comefrom = isset($params['comefrom']) ? $params['comefrom'] : '';
- $bindRes = $this->userBindCompany($params);//绑定门店
- if (!$bindRes['status']) {
- throw new Exception($bindRes['msg']);
- }
- $where['user_id'] = $userId;
- $where['company_id'] = $companyId;
- $userWallet = Db::name('user_wallet')->where($where)->find();
- $time = time();
- if (empty($userWallet)) {
- $userWalletData = [
- 'user_id' => $userId,
- 'company_id' => $companyId,
- 'staff_id' => 0,
- 'money' => 0.00,
- 'address' => '',
- 'createtime' => $time,
- 'comefrom' => $comefrom,
- 'remark' => '',
- ];
- $userWalletRes = Db::name('user_wallet')->insertGetId($userWalletData);
- if (!$userWalletRes) {
- throw new Exception('生成钱包失败');
- }
- }
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- }
- return $result;
- }
- /**
- * 预约消息
- * @return void
- */
- public function msgPreOrder($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '操作成功',
- 'data' => [],
- ];
- try {
- $preOrderId = isset($params['pre_order_id']) ? $params['pre_order_id'] : 0;
- $p = 'pre_order';
- $u = 'user';
- $c = 'company';
- $s = 'servicetype';
- $field = $p.'.*,'.$u.'.mini_openid,'.$c.'.name as `company_name`,'.$s.'.title as `service_title`';
- $where[$p.'.id'] = $preOrderId;
- $preOrder = Db::name($p)->alias($p)->field($field)
- ->join($u,$u.'.id = '.$p.'.user_id','LEFT')
- ->join($c,$c.'.id = '.$p.'.company_id','LEFT')
- ->join($s,$s.'.id = '.$p.'.servicetype_id','LEFT')
- ->where($where)->find();
- if (empty($preOrder)) {
- throw new Exception('未找到预约信息');
- }
- $statusArr = model('PreOrder')->getPreOrderStatusList();
- $statusText = isset($statusArr[$preOrder['pre_order_status']]) ? $statusArr[$preOrder['pre_order_status']] : '';
- $wechatTemplate = config('param.wechat_template');
- $preOrderMsg = isset($wechatTemplate['pre_order']) ? $wechatTemplate['pre_order'] : [];
- $wechatSetting = config('param.wechat_setting');
- if ($wechatSetting == 'release') {//正式
- /*预约时间:{{time2.DATA}}
- 网点名称: {{thing3.DATA}}
- 类型: {{thing1.DATA}}*/
- $data = [//数据内容
- "time2" => ["value" => date('Y年m月d日 H:i',$preOrder['pre_time'])],
- "thing3" => ["value" => $preOrder['company_name'] .'的预约'.$statusText],
- "thing1" => ["value" => $preOrder['service_title']],
- ];
- } else {
- /*预约日期 {{time1.DATA}}
- 场地{{thing8.DATA}}
- 预约类型{{thing7.DATA}}
- 顾客称号{{thing3.DATA}}*/
- $data = [//数据内容
- "time1" => ["value" => date('Y年m月d日 H:i',$preOrder['pre_time'])],
- "thing8" => ["value" => $preOrder['company_name'] .'的预约'.$statusText],
- "thing7" => ["value" => $preOrder['service_title']],
- "thing3" => ["value" => $preOrder['name']],
- ];
- }
- $paramsData = $preOrderMsg;
- $paramsData['mini_openid'] = $preOrder['mini_openid'];
- $paramsData['data'] = $data;
- $msgRes = $this->wechatMessageSend($paramsData);
- if (!$msgRes['status']) {
- throw new Exception($msgRes['msg']);
- }
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- $errorData = [
- 'pre_order_id' => $preOrderId,
- 'params' => isset($paramsData) ? $paramsData : [],
- 'error' => $result['msg'],
- ];
- Log::error(json_encode($errorData));
- }
- return $result;
- }
- /**
- * 订单消息
- * @return void
- */
- public function msgOrder($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '操作成功',
- 'data' => [],
- ];
- try {
- $orderId = isset($params['order_id']) ? $params['order_id'] : 0;
- $o = 'order';
- $u = 'user';
- $where[$o.'.id'] = $orderId;
- $field = $o.'.*,'.$u.'.mini_openid';
- $order = Db::name('order')->alias($o)->field($field)
- ->join($u,$u.'.id = '.$o.'.user_id','LEFT')
- ->where($where)->find();
- if (empty($order)) {
- throw new Exception('未找到订单信息');
- }
- $statusArr = model('Order')->getStatusList();
- $statusText = isset($statusArr[$order['status']]) ? $statusArr[$order['status']] : '';
- $wechatTemplate = config('param.wechat_template');
- $orderMsg = isset($wechatTemplate['order']) ? $wechatTemplate['order'] : [];
- $wechatSetting = config('param.wechat_setting');
- if ($wechatSetting == 'release') {//正式
- /*工单号: {{character_string5.DATA}}
- 状态: {{phrase4.DATA}}
- 下单时间:{{time8.DATA}}*/
- $data = [//数据内容
- "character_string5" => ["value" => $order['orderno']],
- "phrase4" => ["value" => $statusText],
- "time8" => ["value" => date('Y年m月d日 H:i:s',$order['createtime'])],
- ];
- } else {
- /*工单号{{character_string1.DATA}}
- 处理进度 {{phrase4.DATA}}
- 提交时间 {{time2.DATA}}*/
- $data = [//数据内容
- "character_string1" => ["value" => $order['orderno']],
- "phrase4" => ["value" => $statusText],
- "time2" => ["value" => date('Y年m月d日 H:i:s',$order['createtime'])],
- ];
- }
- $orderMsg['page'] = $orderMsg['page'].'?id='.$order['id'];
- $paramsData = $orderMsg;
- $paramsData['mini_openid'] = $order['mini_openid'];
- $paramsData['data'] = $data;
- $msgRes = $this->wechatMessageSend($paramsData);
- if (!$msgRes['status']) {
- throw new Exception($msgRes['msg']);
- }
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- $errorData = [
- 'order_id' => $orderId,
- 'params' => isset($paramsData) ? $paramsData : [],
- 'error' => $result['msg'],
- ];
- Log::error(json_encode($errorData));
- }
- return $result;
- }
- /**
- * 微信消息发送
- * @return void
- */
- public function wechatMessageSend($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '操作成功',
- 'data' => [],
- ];
- return $result;
- try {
- $client = new Client();
- $tk = getAccessToken();
- $uri = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$tk;
- $jsonData = [
- "touser" => isset($params['mini_openid']) ? $params['mini_openid'] : '',//openid
- "template_id" => isset($params['template_id']) ? $params['template_id'] : '',//模版ID
- "page" => isset($params['page']) ? $params['page'] : '', //跳转地址
- "miniprogram_state" => isset($params['miniprogram_state']) ? $params['miniprogram_state'] : '',//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
- "lang" => isset($params['lang']) ? $params['lang'] : 'zh_CN', //支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
- "data" => isset($params['data']) ? $params['data'] : [], //数据
- ];
- $res = $client->request('POST', $uri, ['json' => $jsonData]);
- $returnResJson = $res->getBody()->getContents();
- $returnRes = json_decode($returnResJson, true);
- if ($returnRes['errcode'] != 0) {
- throw new Exception($returnRes['errmsg']);
- }
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- }
- return $result;
- }
- /**
- * 套餐卡券购买
- * @return array
- */
- public function packageCoupons($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '',
- 'data' => [],
- ];
- try {
- $packageId = isset($params['package_id']) ? $params['package_id'] : 0;
- $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- $payOrderId= isset($params['payorder_id']) ? $params['payorder_id'] : 0;
- //赠送优惠券
- $pg = 'package_gift';
- $c = 'coupons';
- $field = $pg.'.*,'.$c.'.name,'.$c.'.info,'.$c.'.days';
- $packageGiftWhere['package_id'] = $packageId;
- $packageGiftWhere[$c.'.status'] = 1;
- $rechargeGift = Db::name($pg)->alias($pg)->field($field)
- ->join($c,$c.'.id = '.$pg.'.coupon_id','LEFT')->where($packageGiftWhere)->select();
- $time = time();
- foreach ($rechargeGift as $k => $v) {
- $checkCode[] = Random::alnum(8);
- }
- $orderService = new OrderService();//防重复
- $checkCodeRes = $orderService->getCheckCoupons($checkCode);
- foreach ($rechargeGift as $key => $value) {
- $endtime = $value['days'] == 0 ? 0 : $time + 86400 * $value['days'];
- if ($checkCodeRes['status']) {
- $checkCodeStr = isset($checkCodeRes['data'][$key]) ? $checkCodeRes['data'][$key] : '';
- } else {
- $checkCodeStr = Random::alnum(8);
- }
- $userCouponsData[] = [
- 'check_code' => $checkCodeStr,
- 'user_id' => $userId,
- 'company_id' => $companyId,
- 'coupons_id' => $value['coupon_id'],
- 'coupon_name' => $value['name'],
- 'coupon_info' => $value['info'],
- 'createtime' => $time,
- 'endtime' => $endtime,
- 'number' => $value['number'],
- 'remain' => $value['number'],
- 'payorder_id' => $payOrderId,
- 'getfrom' => '购买套餐',
- ];
- }
- $userCouponsRes = Db::name('user_coupons')->insertAll($userCouponsData);
- if (!$userCouponsRes) {
- throw new Exception('套餐券获取失败');
- }
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- }
- return $result;
- }
- }
|