UserSynth.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace app\common\service;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use app\common\model\ActivityApplyItem;
  7. use app\common\model\ArticleIntro;
  8. use app\common\model\BillApply;
  9. use app\common\model\BillHeader;
  10. use app\common\model\DatumIntro;
  11. use app\common\model\DeliveryAddress;
  12. use app\common\model\SupplierGoods;
  13. use app\common\model\UserSearch;
  14. use app\common\model\UserVitality;
  15. use app\common\model\VideoIntro;
  16. use think\cache\driver\Redis;
  17. use think\Db;
  18. use Dm\Request\V20151123 as Dm;
  19. /**
  20. * 会员身综合管理
  21. * Class UserIdentity
  22. */
  23. class UserSynth extends SerBase
  24. {
  25. public static $ret_val = ['code' => 200 , 'msg'=>'ok','extend'=>''];
  26. // 会员活跃度
  27. public static function vitality($user_id,$type)
  28. {
  29. if($type === false) return false;
  30. $type++;
  31. $redis = new Redis();
  32. $check_key = date('Y-m-d').'_'.$user_id;
  33. // 当天是否登录
  34. if(!$redis->get($check_key)) {
  35. $redis->set($check_key,86400);
  36. $year = date('Y');
  37. $month = date('m');
  38. $day = date('d');
  39. $vitality_info= [
  40. 'user_id'=>$user_id,
  41. 'year'=>$year,
  42. 'month'=>$month,
  43. 'day'=>$day,
  44. 'day_time'=>date('Y-m-d'),
  45. 'create_at'=>date('Y-m-d H:i:s'),
  46. 'type'=>$type
  47. ];
  48. UserVitality::create($vitality_info);
  49. }
  50. }
  51. /**
  52. * 创建申请发票申请记录【申请开票】
  53. * @param $user_id
  54. * @param $order_id
  55. * @param $order_type 订单类型
  56. * @param $bill_info 发票抬头id||发票详情
  57. * @param $add_id 售货地址id
  58. * @param $send_type 物流类型1点子发票2纸质发票
  59. * @param $remark 备注
  60. * @param $order_pay 订单是否完成支付
  61. * @return array
  62. */
  63. public static function buildBillApply($user_id, $order_id, $order_type,$bill_info,$add_id,$send_type,$remark,$order_pay = 1)
  64. {
  65. $order_table = [
  66. 1=>'level_order',// 会员订单
  67. 2=>'store_order',// 商品订单
  68. 3=>'activity_apply',// 活动订单
  69. ];
  70. Db::startTrans();
  71. try {
  72. $apply_info = [
  73. 'order_id' => $order_id,
  74. 'table_name' => $order_table[$order_type],
  75. 'user_id' => $user_id,
  76. 'remark' => $remark,
  77. 'send_type' => $send_type,
  78. 'order_type' => $order_type,
  79. 'create_at' => date("Y-m-d H:i:s"),
  80. 'order_pay' => $order_pay,
  81. ];
  82. if($add_id) {
  83. $add_info = DeliveryAddress::field('id,pro_name,city_name,county_name,mer_name,detail,phone,name')->where('user_id',$user_id)->where('id',$add_id)->find();
  84. $apply_info['address_info'] = json_encode($add_info->toArray());
  85. }
  86. if(is_array($bill_info)){
  87. $apply_info['header'] = $bill_info['header'];
  88. $apply_info['type'] = $bill_info['type'];
  89. $apply_info['email'] =$bill_info['email'];
  90. // $apply_info['bank'] = $bill_info['bank'];
  91. // $apply_info['card_no'] = $bill_info['card_no'];
  92. // $apply_info['phone'] = $bill_info['phone'];
  93. $apply_info['identify_number'] = $bill_info['identify_number'];
  94. // $apply_info['address'] = $bill_info['address'];
  95. }else if($bill_info){
  96. $bill_header = BillHeader::where('id',$bill_info) ->where('user_id',$user_id)->find();
  97. if(empty($bill_header)) self::exception('发票抬头有误');
  98. $apply_info['header'] = $bill_header->header;
  99. $apply_info['email'] = $bill_header->email;
  100. $apply_info['type'] = $bill_header->type;
  101. $apply_info['identify_number'] = $bill_header->identify_number;
  102. }
  103. $order_info = Db::name($order_table[$order_type])
  104. ->where('user_id',$user_id)
  105. ->where('pay_state',1)
  106. ->where('id',$order_id)
  107. ->find();
  108. if(empty($order_info) && $order_type != 3)self::exception('订单有误');
  109. $check_info = BillApply::where('user_id',$user_id)
  110. ->where('order_id',$order_id)
  111. ->where('is_deleted',0)
  112. ->where('table_name',$order_table[$order_type])
  113. ->find();
  114. if(!empty($check_info) && $check_info->status == 1) self::exception('已开票');
  115. $apply_info = BillApply::create($apply_info);
  116. static::$ret_val['extend'] = $apply_info->id ;
  117. Db::commit();
  118. }catch (\Exception $e) {
  119. Db::rollback();
  120. static::$ret_val['code'] = 201 ;
  121. static::$ret_val['msg'] = $e->getMessage();
  122. }
  123. return static::$ret_val;
  124. }
  125. // 获取所有模块设置
  126. public static function getAllModuleTitle()
  127. {
  128. // 视频
  129. $video = VideoIntro::field('id,title,IF(id,\'video\',0) module')->with(['videoArr'=>function($query){
  130. return $query->field('id,title,video_id');
  131. }])->where(['is_deleted'=>0])->order('id desc')->select()->toArray();
  132. array_walk($video,function (&$v){
  133. $v['children'] = $v['video_arr'];
  134. unset($v['video_arr']);
  135. });
  136. // 资料
  137. $datum = DatumIntro::with(['urlArr'=>function($query){
  138. return $query->field('id,title,datum_id');
  139. }])->where(['is_deleted'=>0])->field('id,title,IF(id,\'datum\',0) module')->order('id desc')->select()->toArray();
  140. array_walk($datum,function (&$v){
  141. $v['children'] = $v['url_arr'];
  142. unset($v['url_arr']);
  143. });
  144. // 图文
  145. $article = ArticleIntro::with(['itemChildren'=>function($query){
  146. return $query->field('id,title,article_id');
  147. }])->field('id,title,IF(id,\'article\',0) module')->where(['is_deleted'=>0])->order('id asc')->select()->toArray();
  148. array_walk($article,function (&$v){
  149. $v['children'] = $v['item_children'];
  150. unset($v['item_children']);
  151. });
  152. $supplier_goods = SupplierGoods::alias('g')->field('g.id,g.supplier_id,g.name title,IF(g.id,\'supplier\',0) module')->where(['g.is_deleted'=>0])
  153. ->leftJoin('Supplier s', 's.id = g.supplier_id')->select()->toArray();
  154. return array_merge($video,$datum,$article,$supplier_goods);
  155. // return compact(['video','datum','article','supplier_goods']);
  156. }
  157. // 推荐
  158. public static function getRecommendList($user_id,$module,$page,$page_num)
  159. {
  160. $module_set = [
  161. 'press'=>[
  162. 'type'=> 4,
  163. 'table'=>'dd_press',
  164. 'index_field'=>'name',
  165. 'select_field'=>'a.id,a.title,a.label,a.images,a.read_num,a.transmit_num,a.user_id,a.create_at,IFNULL(is_recommend,0) is_recommend',
  166. ]
  167. ];
  168. $search_log = UserSearch::getSearchTitle($user_id,$module_set[$module]['type']);
  169. $search_arr = [];
  170. $where_str = 'b.id > 0';
  171. foreach ($search_log as $t){
  172. $search_arr[] = " b.$module_set[$module][\"index_field\"] like '".'%'.$t."%'" .' ';
  173. $search_arr[] = " b.label like '".'%'.$t."%'" .' ';
  174. }
  175. if(!empty($search_arr)) $where_str = implode(' OR ',$search_arr);
  176. switch ($module)
  177. {
  178. case "press":
  179. break;
  180. }
  181. }
  182. // 短信发送
  183. public static function phoneMessageSend($phone,$key,$data)
  184. {
  185. $mud_arr = [];
  186. $mud_arr[0] = [
  187. 'name'=> '活动举办 会前1天',//模板名称
  188. 'tem_code'=> 'SMS_460985703',// 模板code
  189. 'param' => [
  190. 'ActivityName'=>isset($data['ActivityName']) ? $data['ActivityName'] : '' ,
  191. 'time'=>isset($data['time']) ? $data['time'] : '' ,
  192. 'adress'=>isset($data['adress']) ? $data['adress'] : '' ,
  193. 'url'=> isset($data['url']) ? $data['url'] : '' ,
  194. ]
  195. ];
  196. $mud_arr[1] = [
  197. 'name'=> '免费票有审核,审核成功',//模板名称
  198. 'tem_code'=> 'SMS_460970747',// 模板code
  199. 'param' => [
  200. 'ActivityName'=>isset($data['ActivityName']) ? $data['ActivityName'] : '' ,
  201. 'url'=> isset($data['url']) ? $data['url'] : '' ,
  202. ]
  203. ];
  204. $mud_arr[2] = [
  205. 'name'=> ' 后台主动修改时间地点短信短信通知',//模板名称
  206. 'tem_code'=> 'SMS_461030378',// 模板code
  207. 'param' => [
  208. 'ActivityName'=>isset($data['ActivityName']) ? $data['ActivityName'] : '' ,
  209. 'time'=>isset($data['time']) ? $data['time'] : '' ,
  210. 'adress'=>isset($data['adress']) ? $data['adress'] : '' ,
  211. ]
  212. ];
  213. AlibabaCloud::accessKeyClient('LTAI5tJ5p12drZegeWVG33xZ', '82UWAiY5e5wH8tSkRvMtqVoGO0h8SB')->regionId('cn-hangzhou')->asDefaultClient();
  214. try {
  215. $result = AlibabaCloud::rpc()
  216. ->product('Dysmsapi')
  217. ->version('2017-05-25')
  218. ->action('SendSms')
  219. ->method('POST')
  220. ->host('dysmsapi.aliyuncs.com')
  221. ->options([
  222. 'query' => [
  223. 'RegionId' => "cn-hangzhou",
  224. 'PhoneNumbers' => $phone,
  225. 'SignName' => "搞一下汽车电子",
  226. 'TemplateCode' => $mud_arr[$key]['tem_code'],
  227. 'TemplateParam' => json_encode($mud_arr[$key]['param']),
  228. ],
  229. ])->request();
  230. $result = $result->toArray();
  231. if($result['Code'] != "OK") return ['code'=>201,'msg'=>'发送失败'];
  232. } catch (ClientException $e) {
  233. return ['code'=>201,'msg'=>$e->getErrorMessage().PHP_EOL];
  234. } catch (ServerException $e) {
  235. return ['code'=>201,'msg'=>$e->getErrorMessage().PHP_EOL];
  236. }catch (\Exception $e){
  237. return ['code'=>201,'msg'=>$e->getMessage()];
  238. }
  239. return ['code'=>200,'msg'=>'发送成功'];
  240. }
  241. // 邮件推送
  242. public static function emailSend($email,$title)
  243. {
  244. require_once env('root_path').'vendor/aliyunmail/aliyun-php-sdk-core/Config.php';
  245. $iClientProfile = \DefaultProfile::getProfile("cn-hangzhou", "LTAI5tJ5p12drZegeWVG33xZ", "82UWAiY5e5wH8tSkRvMtqVoGO0h8SB");
  246. $client = new \DefaultAcsClient($iClientProfile);
  247. $request = new Dm\SingleSendMailRequest();
  248. $request->setAccountName("gyx@yzm.gyxqcdz.com");
  249. $request->setFromAlias("活动提示");
  250. $request->setAddressType(1);
  251. $request->setTagName("activity");
  252. $request->setReplyToAddress("true");
  253. $request->setToAddress($email);
  254. $request->setSubject("活动提示");
  255. $html_body = '您报名的活动'.$title.'已发生变更。';
  256. $request->setHtmlBody($html_body);
  257. $send_res = $client->getAcsResponse($request);
  258. return ['code'=>200,'msg'=>'发送成功'];
  259. }
  260. //门票邮件推送
  261. public static function ticketSend($ticket_id)
  262. {
  263. $ticket_info = ActivityApplyItem::where('id',$ticket_id)->find()->toArray();
  264. if(!$ticket_info['email'])['code'=>201,'msg'=>'邮件为空'];
  265. $email = $ticket_info['email'];
  266. $title = \app\common\model\Activity::where('id',$ticket_info['act_id'])->value('title');
  267. require_once env('root_path').'vendor/aliyunmail/aliyun-php-sdk-core/Config.php';
  268. $iClientProfile = \DefaultProfile::getProfile("cn-hangzhou", "LTAI5tJ5p12drZegeWVG33xZ", "82UWAiY5e5wH8tSkRvMtqVoGO0h8SB");
  269. $client = new \DefaultAcsClient($iClientProfile);
  270. $request = new Dm\SingleSendMailRequest();
  271. $request->setAccountName("gyx@yzm.gyxqcdz.com");
  272. $request->setFromAlias("活动提示");
  273. $request->setAddressType(1);
  274. $request->setTagName("activity");
  275. $request->setReplyToAddress("true");
  276. $request->setToAddress($email);
  277. $request->setSubject("活动提示");
  278. $act_title = $title;
  279. $num = 1;
  280. $qr_code = $ticket_info['qr_code'];
  281. $html_body = file_get_contents('/ticket_info.php');
  282. var_dump($html_body);die();
  283. $request->setHtmlBody($html_body);
  284. $send_res = $client->getAcsResponse($request);
  285. return ['code'=>200,'msg'=>'发送成功'];
  286. }
  287. }