Unmarket.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 联盟促销
  7. */
  8. namespace app\allwin\controller\home;
  9. use app\common\facade\Inform;
  10. use think\facade\Request;
  11. use app\common\controller\Official;
  12. use app\common\event\User;
  13. use app\common\facade\WechatMp;
  14. use app\allwin\model\AllwinUnmarket;
  15. use app\allwin\model\AllwinUnmarketOrder;
  16. use app\allwin\model\AllwinUnmarketStore;
  17. use app\allwin\model\AllwinUnmarketBooking;
  18. use app\allwin\model\AllwinUnmarketReward;
  19. use app\allwin\model\AllwinUnmarketVerif;
  20. use app\allwin\model\MchId;
  21. use app\common\facade\WechatPay;
  22. use app\allwin\model\AllwinStore;
  23. class Unmarket extends Official{
  24. /**
  25. * 分享赚钱营销
  26. */
  27. public function index($id){
  28. if(request()->isAjax()){
  29. $data = [
  30. 'id' => input('post.id/d'),
  31. 'member_miniapp_id' => $this->member_miniapp_id,
  32. 'telphone' => input('post.telphone/s','','htmlspecialchars'),
  33. 'telname' => input('post.telname/s','','htmlspecialchars'),
  34. 'uid' => $this->user->id,
  35. ];
  36. $validate = $this->validate($data,'Unmarket.order');
  37. if(true !== $validate){
  38. return json(['code'=>0,'msg'=>$validate]);
  39. }
  40. //判断是否已购买过
  41. $order = AllwinUnmarketOrder::where(['member_miniapp_id' => $this->member_miniapp_id,'share_id' => $data['id'],'uid' => $this->user->id,'state' => 1])->count();
  42. if($order > 0){
  43. return json(['code'=>0,'msg'=>'您已下单,不允许重复下单']);
  44. }
  45. //判断是否邀请用户
  46. $ucode = model('SystemUser')->isInvite(User::getUcode());
  47. $data['share_uid'] = 0;
  48. if($ucode){
  49. if($this->user['id'] != $ucode){
  50. $data['share_uid'] = $ucode;
  51. }
  52. }
  53. $order_no = $this->user->invite_code.order_no();
  54. $data['order_no'] = $order_no;
  55. $result = AllwinUnmarketOrder::createOrder($data);
  56. if($result){
  57. $unorder['miniapp_id'] = $this->member_miniapp_id;
  58. $unorder['name'] = $result->title;
  59. $unorder['order_no'] = $order_no;
  60. $unorder['total_fee'] = $result->price*100;
  61. $unorder['notify_url'] = Request::root(true).'/api-'.$this->member_miniapp_id.'/4/allwin-unmarket-notify.html';
  62. $setting = model('AllwinConfig')->getConfig($this->member_miniapp_id);
  63. if ($setting->is_psp == 1) {
  64. $mid = MchId::getMch(0,$this->member_miniapp_id);
  65. $unorder['mchid'] = $mid->mchid;
  66. }
  67. $unorder['openid'] = $this->user->official_uid;
  68. $ispay = WechatPay::orderPay($unorder,true);
  69. if ($ispay['code'] == 0) {
  70. return json(['code'=>0,'return_code'=>'FAIL','msg' => $ispay['msg']]);
  71. }
  72. return json(['code'=>200,'msg'=>'操作成功','data'=>$ispay['data']]);
  73. }else{
  74. return json(['code'=>0,'msg'=>'活动不存在']);
  75. }
  76. }else{
  77. $condition['id'] = $id;
  78. $info = AllwinUnmarket::where($condition)->find();
  79. if(empty($info)){
  80. $this->error("404 NOT FOUND");
  81. }
  82. $imgs = json_decode($info['imgs'],true);
  83. $info['imgs'] = array_values_unset($info->img,$imgs);
  84. $view['info'] = $info;
  85. $view['order'] = AllwinUnmarketOrder::where(['share_id' => $info->id,'state' => 1])->order('id desc')->select(); //订单
  86. $view['order_num'] = count($view['order']);
  87. $view['store'] = AllwinUnmarketStore::where(['union_id' => $info->id])->order('id desc')->select(); //好店
  88. $view['award_amout'] = AllwinUnmarketReward::where(['union_id' => $info->id])->sum('money'); //计算福利总金额
  89. $view['award'] = AllwinUnmarketReward::where(['union_id' => $info->id])->order('money desc')->limit(50)->select(); //福利排行榜
  90. $view['weconfig'] = WechatMp::jsApiList($this->member_miniapp_id);
  91. $view['post_url'] = urls('Allwin/unmarket/index',$this->member_miniapp_id,['id' =>$id,'ucode' => $this->ucode]);
  92. $view['share_url'] = urls('Allwin/unmarket/index',$this->member_miniapp_id,['id' =>$id,'ucode' => $this->user->invite_code]);
  93. $view['share_rcode'] = action("common/library/qrcode/create",['url' => $view['share_url'],'qrname'=>'unmarket_qrcode_'.$id.'_'.$this->user['invite_code']],'facade');
  94. AllwinUnmarket::where($condition)->setInc('views',rand(1,5));
  95. $this->view->engine->layout(false);
  96. return view()->assign($view);
  97. }
  98. }
  99. /**
  100. * 联盟商家
  101. */
  102. public function store(int $id,int $unid){
  103. $info = AllwinUnmarket::where(['id' => $unid])->field(['title,share_img,share_title,share_text'])->find();
  104. if(empty($info)){
  105. $this->error("404 NOT FOUND");
  106. }
  107. $store = AllwinUnmarketStore::where(['id' => $id,'union_id' => $unid])->find();
  108. if(empty($store)){
  109. $this->error("404 NOT FOUND");
  110. }
  111. $store->imgs = array_values_unset($store->img,json_decode($store->imgs,true));
  112. $view['store'] = $store;
  113. $view['info'] = $info;
  114. $view['weconfig'] = WechatMp::jsApiList($this->member_miniapp_id);
  115. $view['share_url'] = urls('Allwin/unmarket/index',$this->member_miniapp_id,['id' =>$unid,'ucode' => $this->user->invite_code]);
  116. $this->view->engine->layout(false);
  117. return view()->assign($view);
  118. }
  119. /**
  120. * 我的订单
  121. */
  122. public function order(int $id){
  123. if (request()->isAjax()) {
  124. $data = [
  125. 'id' => $id,
  126. 'store_id' => input('post.store_id/d'),
  127. 'member_miniapp_id' => $this->member_miniapp_id,
  128. 'uid' => $this->user->id,
  129. ];
  130. $validate = $this->validate($data,'Unmarket.okpass');
  131. if(true !== $validate){
  132. return json(['code'=>0,'msg'=>$validate]);
  133. }
  134. //判断是否付款
  135. $order = AllwinUnmarketOrder::where(['uid' => $this->user->id,'share_id' => $id,'state' => 1])->count();
  136. if($order <= 0){
  137. return json(['code'=>0,'msg'=>'你没有下单,禁止核销']);
  138. }
  139. //判断是否联盟
  140. $store = AllwinUnmarketStore::where(['member_miniapp_id' => $this->member_miniapp_id,'union_id' => $id,'id' => $data['store_id']])->find();
  141. if(empty($store)){
  142. return json(['code'=>0,'msg'=>'未找到核销商家,禁止核销']);
  143. }
  144. //判断是否重复核销
  145. $verif = AllwinUnmarketVerif::where(['uid' => $this->user->id,'union_id' => $id,'store_id' => $data['store_id']])->count();
  146. if($verif > 0){
  147. return json(['code'=>0,'msg'=>'统一店铺,禁止重复核销']);
  148. }
  149. $verif_data['member_miniapp_id'] = $this->member_miniapp_id;
  150. $verif_data['uid'] = $this->user->id;
  151. $verif_data['union_id'] = $id;
  152. $verif_data['store_id'] = $data['store_id'];
  153. $verif_data['times'] = time();
  154. $rel = AllwinUnmarketVerif::insert($verif_data);
  155. if(empty($rel)){
  156. return json(['code'=>0,'msg'=>'核销失败']);
  157. }else{
  158. $modelStore = AllwinStore::where(['id' => $store->store_id])->find();
  159. if($modelStore){
  160. //通知店主到微信
  161. Inform::sms($modelStore->manage_uid,$this->member_miniapp_id,['title' =>'业务进展通知','type' => '订单核销','content' =>'您有新订单已经成功核销','state' => '成功']);
  162. }
  163. //通知申请者到微信
  164. Inform::sms($this->user->id,$this->member_miniapp_id,['title' =>'业务进展通知','type' => '订单核销','content' =>'您的订单已经成功核销','state' => '成功']);
  165. return json(['code'=>200,'msg'=>'您已成功核销']);
  166. }
  167. }else{
  168. $view['info'] = AllwinUnmarket::where(['id' => $id])->field(['id,telphone,mycode,tips,title,share_img,share_title,share_text,start_time,end_time'])->find();
  169. if(empty($view['info'])){
  170. $this->error("404 NOT FOUND");
  171. }
  172. $is_order = AllwinUnmarketOrder::where(['uid' => $this->user->id,'share_id' => $id,'state' => 1])->find();
  173. $order = [];
  174. if(empty($is_order)){
  175. $view['store'] = [];
  176. }else{
  177. $view['store'] = AllwinUnmarketStore::where(['member_miniapp_id' => $this->member_miniapp_id,'union_id' => $id])->select();
  178. $verif = AllwinUnmarketVerif::where(['uid' => $this->user->id,'union_id' => $id])->field('store_id')->select()->toArray();
  179. $order = array_column($verif,'store_id');
  180. }
  181. $view['order'] = $order;
  182. $this->view->engine->layout(false);
  183. return view()->assign($view);
  184. }
  185. }
  186. /**
  187. * 联系客服
  188. */
  189. public function service(){
  190. if (request()->isAjax()) {
  191. $data = [
  192. 'member_miniapp_id' => $this->member_miniapp_id,
  193. 'union_id' => input('post.union_id/d', '', 'htmlspecialchars'),
  194. 'name' => input('post.storename/s', '', 'htmlspecialchars'),
  195. 'telphone' => input('post.telphone/s', '', 'htmlspecialchars'),
  196. 'telname' => input('post.telname/s', '', 'htmlspecialchars'),
  197. ];
  198. $validate = $this->validate($data, 'Unmarket.service');
  199. if (true !== $validate) {
  200. return json(['code'=>0,'msg'=>$validate]);
  201. }
  202. //读取所属活动
  203. $info = AllwinUnmarket::where(['id' => $data['union_id'],'member_miniapp_id' => $this->member_miniapp_id])->field('id')->find();
  204. if (empty($info)) {
  205. return json(['code'=>0,'msg'=>'活动不存在']);
  206. }
  207. $param = [
  208. 'member_miniapp_id' => $this->member_miniapp_id,
  209. 'union_id' => $info->id,
  210. 'store_name' => $data['name'],
  211. 'name' => $data['telname'],
  212. 'telphone' => $data['telphone'],
  213. ];
  214. $result = AllwinUnmarketBooking::edit($param);
  215. if ($result) {
  216. return json(['code'=>200,'msg'=>'我们客服会在8小时内与你联系']);
  217. } else {
  218. return json(['code'=>0,'msg'=>'提交错误,请重试']);
  219. }
  220. } else {
  221. $this->error('404 Not Found');
  222. }
  223. }
  224. }