Package.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. $fileName = md5($package_id);
  69. $fileUrl = '/uploads/package/'.$fileName.'.png';
  70. Db::name('package')->where('id',$package_id)->update(['mini_code'=>$fileUrl]);
  71. if (isset($data['type']) && $data['type'] == 2) {
  72. $gift_data = input('gift_data','','trim');
  73. $gift_data = json_decode(htmlspecialchars_decode($gift_data),true);
  74. if(is_array($gift_data) && !empty($gift_data)){
  75. $package_gift = [];
  76. foreach($gift_data as $key => $val){
  77. $package_gift[] = [
  78. 'package_id' => $package_id,
  79. 'coupon_id' => $val['coupon_id'],
  80. 'number' => $val['number'],
  81. ];
  82. }
  83. if(!empty($package_gift)){
  84. $rs_gift = Db::name('package_gift')->insertAll($package_gift);
  85. if($rs_gift === false){
  86. Db::rollback();
  87. $this->error('添加失败');
  88. }
  89. }
  90. }
  91. }
  92. Db::commit();
  93. //生成小程序码
  94. $this->getMiniCode($package_id,[],$this->auth->company_id);
  95. $this->success('添加成功');
  96. } catch (Exception $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. }
  100. }
  101. //上下架
  102. public function changestatus(){
  103. //验证
  104. if($this->auth->type != 1){
  105. $this->error('只有门店老板才能设置');
  106. }
  107. $id = input('id',0);
  108. $status = Db::name('package')->where('id',$id)->value('status');
  109. $new_status = $status == 1 ? 0 : 1;
  110. $info = Db::name('package')->where('id',$id)->update(['status'=>$new_status,'updatetime'=>time()]);
  111. $this->success();
  112. }
  113. //详情
  114. public function info(){
  115. $id = input('id',0);
  116. $info = Db::name('package')->alias('p')
  117. ->field('p.*,type.title as servicetype_title')
  118. ->join('servicetype type','p.servicetype_id = type.id','LEFT')
  119. ->where('p.id',$id)->find();
  120. //追加赠送
  121. if(!empty($info)){
  122. $gift = Db::name('package_gift')->alias('gift')
  123. ->field('gift.*,coupons.name,coupons.info,coupons.days')
  124. ->join('coupons','gift.coupon_id = coupons.id','LEFT')
  125. ->where('gift.package_id',$id)
  126. ->where('coupons.status',1)
  127. ->select();
  128. $info['gift'] = [];
  129. if (!empty($gift)) {
  130. foreach($gift as $k => $v){
  131. $info['gift'][] = $v;
  132. }
  133. }
  134. }
  135. $info = info_domain_image($info,['images','content_images']);
  136. $this->success(1,$info);
  137. }
  138. //编辑
  139. public function edit(){
  140. Db::startTrans();
  141. try {
  142. //验证
  143. if($this->auth->type != 1){
  144. throw new Exception('只有门店老板才能设置');
  145. }
  146. $id = input('id','');
  147. $check = Db::name('package')->where('id',$id)->where('company_id',$this->auth->company_id)->lock(true)->find();
  148. if(empty($check)){
  149. throw new Exception('不存在的套餐');
  150. }
  151. //
  152. $field = ['title','info','servicetype_id','images','price','oldprice','num','content','content_images'];
  153. $data = request_post_hub($field);
  154. $data['updatetime'] = time();
  155. $fileName = md5($id);
  156. $fileUrl = '/uploads/package/'.$fileName.'.png';
  157. $data['mini_code'] = $fileUrl;
  158. $packageRes = Db::name('package')->where('id',$id)->update($data);
  159. if (!$packageRes) {
  160. throw new Exception('套餐编辑失败');
  161. }
  162. if ($check['type'] == 2) {
  163. $packageGiftWhere['package_id'] = $id;
  164. $packageGiftData = Db::name('package_gift')->where($packageGiftWhere)->select();
  165. $gift_data = input('gift_data','','trim');
  166. $gift_data = json_decode(htmlspecialchars_decode($gift_data),true);
  167. if(is_array($gift_data) && !empty($gift_data)) {
  168. $packageGiftCouponIds = array_column($packageGiftData,'coupon_id');
  169. $postCouponIds = array_column($gift_data,'coupon_id');
  170. $package_gift = [];
  171. $same = [];
  172. $different = [];
  173. foreach ($gift_data as $key => $val) {
  174. if (in_array($val['coupon_id'], $packageGiftCouponIds)) {
  175. $same[] = [
  176. 'package_id' => $id,
  177. 'coupon_id' => $val['coupon_id'],
  178. 'number' => $val['number'],
  179. ];
  180. } else {
  181. $different[] = [
  182. 'package_id' => $id,
  183. 'coupon_id' => $val['coupon_id'],
  184. 'number' => $val['number'],
  185. ];
  186. }
  187. }
  188. if (!empty($same)) {//相同的更新
  189. foreach ($same as $sameKey => $sameVal) {
  190. $sameWhere['package_id'] = $sameVal['package_id'];
  191. $sameWhere['coupon_id'] = $sameVal['coupon_id'];
  192. $sameData['number'] = $sameVal['number'];
  193. Db::name('package_gift')->where($sameWhere)->update($sameData);
  194. }
  195. }
  196. if (!empty($different)) {//新增的添加
  197. $packageGiftRes = Db::name('package_gift')->where($sameWhere)->insertAll($different);
  198. if (!$packageGiftRes) {
  199. throw new Exception('套餐卡券添加失败');
  200. }
  201. }
  202. //多余的删除
  203. $delIds = array_diff($packageGiftCouponIds,$postCouponIds);
  204. if (!empty($delIds)) {
  205. $packageGiftDelWhere['package_id'] = $id;
  206. $packageGiftDelWhere['coupon_id'] = ['in',$delIds];
  207. Db::name('package_gift')->where($packageGiftDelWhere)->delete();
  208. }
  209. } else {
  210. if (!empty($packageGiftData)) {
  211. $packageGiftDelRes = Db::name('package_gift')->where($packageGiftWhere)->delete();
  212. if (!$packageGiftDelRes) {
  213. throw new Exception('删除卡券失败');
  214. }
  215. }
  216. }
  217. }
  218. Db::commit();
  219. //生成小程序码
  220. $this->getMiniCode($id,$check,$this->auth->company_id);
  221. $this->success('编辑成功');
  222. } catch (Exception $e) {
  223. Db::rollback();
  224. $this->error($e->getMessage());
  225. }
  226. }
  227. //删除
  228. public function delete(){
  229. //验证
  230. if($this->auth->type != 1){
  231. $this->error('只有门店老板才能设置');
  232. }
  233. $id = input('id','');
  234. $check = Db::name('package')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  235. if(empty($check)){
  236. $this->error('不存在的套餐');
  237. }
  238. Db::name('package')->where('id',$id)->delete();
  239. Db::name('package_gift')->where('package_id',$id)->delete();
  240. $this->success('删除成功');
  241. }
  242. //仅返回套餐二维码
  243. private function getMiniCode($id,$package = [],$companyid)
  244. {
  245. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  246. if (empty($package) || empty($package['mini_code'])) {
  247. $client = new Client();
  248. $tk = getAccessToken();
  249. $miniCodeConfig = config('param.mini_code');
  250. //$miniCodeConfig['env_version'] = 'trial'; //强制体验版
  251. $miniCodeConfig['scene'] = 'id='.$id.'&shopId='.$companyid;
  252. $res2 = $client->request('POST', 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk, [
  253. 'json' => $miniCodeConfig,
  254. ]);
  255. $fileName = md5($id);
  256. $fileUrl = '/uploads/package/'.$fileName.'.png';
  257. $code = $res2->getBody()->getContents();
  258. file_put_contents(ROOT_PATH.'/public'.$fileUrl,$code);
  259. $miniCode = $httpStr.$fileUrl;
  260. } else {
  261. $miniCode = $httpStr.$package['mini_code'];
  262. }
  263. return $miniCode;
  264. }
  265. //生成视频分享海报
  266. public function showposter() {
  267. $id = input('id', 0, 'intval'); //id
  268. if (!$id) {
  269. $this->error('参数缺失');
  270. }
  271. $info = Db::name('package')->where('id',$id)->find();
  272. if (!$info) {
  273. $this->error('套餐不存在');
  274. }
  275. $inviteimage = $this->getMiniCode($id,$info,$this->auth->company_id);
  276. $package_image = explode(',',$info['images'])[0];
  277. $data = [
  278. [
  279. "left"=> "0px",
  280. "top"=> "0px",
  281. "type"=> "img",
  282. "width"=> "320px",
  283. "height"=> "290px",
  284. "src"=> one_domain_image($package_image)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  285. ],
  286. [
  287. "left"=> "10px",
  288. "top"=> "370px",
  289. "type"=> "img",
  290. "width"=> "45px",
  291. "height"=> "45px",
  292. "src"=> one_domain_image($this->auth->company->image)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  293. ],
  294. [
  295. "left"=> "10px",
  296. "top"=> "330px",
  297. "type"=> "nickname",
  298. "width"=> "166px",
  299. "height"=> "38px",
  300. "size"=> "11px",
  301. "color"=> "#333333",
  302. "content" => (iconv_strlen($info['title'], 'utf-8') <= 12 ? $info['title'] : mb_substr($info['title'], 0, 12) )
  303. ],
  304. [
  305. "left"=> "60px",
  306. "top"=> "385px",
  307. "type"=> "nickname",
  308. "width"=> "166px",
  309. "height"=> "38px",
  310. "size"=> "10px",
  311. "color"=> "#666666",
  312. "content" => (iconv_strlen($this->auth->company->name, 'utf-8') <= 8 ? $this->auth->company->name : mb_substr($this->auth->company->name, 0, 8))
  313. ],
  314. [
  315. "left"=> "195px",
  316. "top"=> "300px",
  317. "type"=> "img",
  318. "width"=> "110px",
  319. "height"=> "110px",
  320. "src"=> httpurllocal($inviteimage)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  321. ],
  322. [
  323. "left"=> "200px",
  324. "top"=> "420px",
  325. "type"=> "nickname",
  326. "width"=> "166px",
  327. "height"=> "38px",
  328. "size"=> "9px",
  329. "color"=> "#666666",
  330. "content" => '长按扫码查看详情',
  331. ],
  332. ];
  333. $data = json_encode($data, 320);
  334. $poster = [
  335. 'id' => 1,
  336. 'title' => '测试',
  337. 'waittext' => '您的专属海报正在拼命生成中,请等待片刻...',
  338. 'bg_image' => '/assets/img/posterbg.png',
  339. 'data' => $data,
  340. 'status' => 'normal',
  341. 'weigh' => 0,
  342. 'createtime' => 1653993709,
  343. 'updatetime' => 1653994259,
  344. ];
  345. $image = new \addons\poster\library\Image();
  346. $imgurl = $image->createPosterImage($poster, $this->auth->getUser());
  347. if (!$imgurl) {
  348. $this->error('生成海报出错');
  349. }
  350. $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;
  351. //echo '<html><body><img src="'.$imgurl.'"></body></html>';exit;
  352. $this->success('', $imgurl);
  353. }
  354. }