Package.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. use think\Exception;
  6. use GuzzleHttp\Client;
  7. /**
  8. * 套餐管理
  9. */
  10. class Package extends Apic
  11. {
  12. protected $noNeedLogin = [];
  13. protected $noNeedRight = '*';
  14. //列表
  15. public function lists(){
  16. $status = input('status',1);
  17. $keyword = input('keyword','');
  18. $where = [
  19. 'p.company_id' => $this->auth->company_id,
  20. 'p.status' => $status,
  21. ];
  22. if(!empty($keyword)){
  23. $where['p.title|p.info'] = ['LIKE','%'.$keyword.'%'];
  24. }
  25. $list = Db::name('package')->alias('p')
  26. ->field('p.*,type.title as servicetype_title')
  27. ->join('servicetype type','p.servicetype_id = type.id','LEFT')
  28. ->where($where)->order('p.id desc')->autopage()->select();
  29. //追加赠送
  30. if(!empty($list)){
  31. $package_ids = array_column($list,'id');
  32. $gift = Db::name('package_gift')->alias('gift')
  33. ->field('gift.*,coupons.name,coupons.info,coupons.days')
  34. ->join('coupons','gift.coupon_id = coupons.id','LEFT')
  35. ->where('gift.package_id','IN',$package_ids)
  36. ->where('coupons.status',1)
  37. ->select();
  38. foreach($list as $key => &$val){
  39. $val['gift'] = [];
  40. foreach($gift as $k => $v){
  41. if($val['id'] == $v['package_id']){
  42. $val['gift'][] = $v;
  43. }
  44. }
  45. }
  46. }
  47. $list = list_domain_image($list,['images','content_images']);
  48. $this->success(1,$list);
  49. }
  50. //新增
  51. public function add(){
  52. Db::startTrans();
  53. try {
  54. //验证
  55. if($this->auth->type != 1){
  56. throw new Exception('只有门店老板才能设置');
  57. }
  58. $field = ['title','info','servicetype_id','images','price','oldprice','num','content','content_images','type'];
  59. $data = request_post_hub($field);
  60. $data['company_id'] = $this->auth->company_id;
  61. $data['createtime'] = time();
  62. $data['updatetime'] = time();
  63. $data['status'] = 1;
  64. $package_id = Db::name('package')->insertGetId($data);
  65. if (!$package_id) {
  66. throw new Exception('添加套餐失败');
  67. }
  68. if (isset($data['type']) && $data['type'] == 2) {
  69. $gift_data = input('gift_data','','trim');
  70. $gift_data = json_decode(htmlspecialchars_decode($gift_data),true);
  71. if(is_array($gift_data) && !empty($gift_data)){
  72. $package_gift = [];
  73. foreach($gift_data as $key => $val){
  74. $package_gift[] = [
  75. 'package_id' => $package_id,
  76. 'coupon_id' => $val['coupon_id'],
  77. 'number' => $val['number'],
  78. ];
  79. }
  80. if(!empty($package_gift)){
  81. $rs_gift = Db::name('package_gift')->insertAll($package_gift);
  82. if($rs_gift === false){
  83. Db::rollback();
  84. $this->error('添加失败');
  85. }
  86. }
  87. }
  88. }
  89. Db::commit();
  90. $this->success('添加成功');
  91. } catch (Exception $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. }
  95. }
  96. public function showposter(){
  97. $id = input('id',1);
  98. $this->getMiniCode($id,$this->auth->company_id);
  99. }
  100. private function getMiniCode($id,$companyid)
  101. {
  102. $package = Db::name('package')->where('id',$id)->find();
  103. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  104. if (empty($package['mini_code'])) {
  105. $client = new Client();
  106. $tk = getAccessToken();
  107. $miniCodeConfig = config('param.mini_code');
  108. //$miniCodeConfig['env_version'] = 'trial'; //强制体验版
  109. $miniCodeConfig['scene'] = 'id='.$id.'&shopId='.$companyid;
  110. $res2 = $client->request('POST', 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk, [
  111. 'json' => $miniCodeConfig,
  112. ]);
  113. $fileName = md5($id);
  114. $fileUrl = '/uploads/package/'.$fileName.'.png';
  115. $code = $res2->getBody()->getContents();
  116. file_put_contents(ROOT_PATH.'/public'.$fileUrl,$code);
  117. $companyRes = Db::name('package')->where('id',$id)->update(['mini_code'=>$fileUrl]);
  118. if ($companyRes === false) {
  119. $this->error('生成套餐小程序码失败');
  120. }
  121. $miniCode = $httpStr.$fileUrl;
  122. } else {
  123. $miniCode = $httpStr.$package['mini_code'];
  124. }
  125. $this->success('success',$miniCode);
  126. }
  127. //上下架
  128. public function changestatus(){
  129. //验证
  130. if($this->auth->type != 1){
  131. $this->error('只有门店老板才能设置');
  132. }
  133. $id = input('id',0);
  134. $status = Db::name('package')->where('id',$id)->value('status');
  135. $new_status = $status == 1 ? 0 : 1;
  136. $info = Db::name('package')->where('id',$id)->update(['status'=>$new_status,'updatetime'=>time()]);
  137. $this->success();
  138. }
  139. //详情
  140. public function info(){
  141. $id = input('id',0);
  142. $info = Db::name('package')->alias('p')
  143. ->field('p.*,type.title as servicetype_title')
  144. ->join('servicetype type','p.servicetype_id = type.id','LEFT')
  145. ->where('p.id',$id)->find();
  146. //追加赠送
  147. if(!empty($info)){
  148. $gift = Db::name('package_gift')->alias('gift')
  149. ->field('gift.*,coupons.name,coupons.info,coupons.days')
  150. ->join('coupons','gift.coupon_id = coupons.id','LEFT')
  151. ->where('gift.package_id',$id)
  152. ->where('coupons.status',1)
  153. ->select();
  154. $info['gift'] = [];
  155. if (!empty($gift)) {
  156. foreach($gift as $k => $v){
  157. $info['gift'][] = $v;
  158. }
  159. }
  160. }
  161. $info = info_domain_image($info,['images','content_images']);
  162. $this->success(1,$info);
  163. }
  164. //编辑
  165. public function edit(){
  166. Db::startTrans();
  167. try {
  168. //验证
  169. if($this->auth->type != 1){
  170. throw new Exception('只有门店老板才能设置');
  171. }
  172. $id = input('id','');
  173. $check = Db::name('package')->where('id',$id)->where('company_id',$this->auth->company_id)->lock(true)->find();
  174. if(empty($check)){
  175. throw new Exception('不存在的套餐');
  176. }
  177. //
  178. $field = ['title','info','servicetype_id','images','price','oldprice','num','content','content_images'];
  179. $data = request_post_hub($field);
  180. $data['updatetime'] = time();
  181. $packageRes = Db::name('package')->where('id',$id)->update($data);
  182. if (!$packageRes) {
  183. throw new Exception('套餐编辑失败');
  184. }
  185. if ($check['type'] == 2) {
  186. $packageGiftWhere['package_id'] = $id;
  187. $packageGiftData = Db::name('package_gift')->where($packageGiftWhere)->select();
  188. $gift_data = input('gift_data','','trim');
  189. $gift_data = json_decode(htmlspecialchars_decode($gift_data),true);
  190. if(is_array($gift_data) && !empty($gift_data)) {
  191. $packageGiftCouponIds = array_column($packageGiftData,'coupon_id');
  192. $postCouponIds = array_column($gift_data,'coupon_id');
  193. $package_gift = [];
  194. $same = [];
  195. $different = [];
  196. foreach ($gift_data as $key => $val) {
  197. if (in_array($val['coupon_id'], $packageGiftCouponIds)) {
  198. $same[] = [
  199. 'package_id' => $id,
  200. 'coupon_id' => $val['coupon_id'],
  201. 'number' => $val['number'],
  202. ];
  203. } else {
  204. $different[] = [
  205. 'package_id' => $id,
  206. 'coupon_id' => $val['coupon_id'],
  207. 'number' => $val['number'],
  208. ];
  209. }
  210. }
  211. if (!empty($same)) {//相同的更新
  212. foreach ($same as $sameKey => $sameVal) {
  213. $sameWhere['package_id'] = $sameVal['package_id'];
  214. $sameWhere['coupon_id'] = $sameVal['coupon_id'];
  215. $sameData['number'] = $sameVal['number'];
  216. Db::name('package_gift')->where($sameWhere)->update($sameData);
  217. }
  218. }
  219. if (!empty($different)) {//新增的添加
  220. $packageGiftRes = Db::name('package_gift')->where($sameWhere)->insertAll($different);
  221. if (!$packageGiftRes) {
  222. throw new Exception('套餐卡券添加失败');
  223. }
  224. }
  225. //多余的删除
  226. $delIds = array_diff($packageGiftCouponIds,$postCouponIds);
  227. if (!empty($delIds)) {
  228. $packageGiftDelWhere['package_id'] = $id;
  229. $packageGiftDelWhere['coupon_id'] = ['in',$delIds];
  230. Db::name('package_gift')->where($packageGiftDelWhere)->delete();
  231. }
  232. } else {
  233. if (!empty($packageGiftData)) {
  234. $packageGiftDelRes = Db::name('package_gift')->where($packageGiftWhere)->delete();
  235. if (!$packageGiftDelRes) {
  236. throw new Exception('删除卡券失败');
  237. }
  238. }
  239. }
  240. }
  241. Db::commit();
  242. $this->success('编辑成功');
  243. } catch (Exception $e) {
  244. Db::rollback();
  245. $this->error($e->getMessage());
  246. }
  247. }
  248. //删除
  249. public function delete(){
  250. //验证
  251. if($this->auth->type != 1){
  252. $this->error('只有门店老板才能设置');
  253. }
  254. $id = input('id','');
  255. $check = Db::name('package')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  256. if(empty($check)){
  257. $this->error('不存在的套餐');
  258. }
  259. Db::name('package')->where('id',$id)->delete();
  260. Db::name('package_gift')->where('package_id',$id)->delete();
  261. $this->success('删除成功');
  262. }
  263. }