SmallProgramController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * Author: 芸众商城 www.yunzshop.com
  4. * Date: 2017/11/8
  5. * Time: 下午4:20
  6. */
  7. namespace app\backend\modules\setting\controllers;
  8. use app\common\components\BaseController;
  9. use app\common\helpers\PaginationHelper;
  10. use app\common\helpers\Url;
  11. use app\common\models\MiniTemplateCorresponding;
  12. use app\common\models\notice\MessageTemp;
  13. use app\backend\modules\setting\controllers\SmallProgramDataController;
  14. use app\backend\modules\setting\controllers\DiyTempController;
  15. use app\common\models\notice\MinAppTemplateMessage;
  16. use app\common\services\notice\SmallProgramNotice;
  17. use app\common\models\MemberMiniAppModel;
  18. class SmallProgramController extends BaseController
  19. {
  20. private $temp_model;
  21. protected $SmallProgramNotice;
  22. public function __construct(){
  23. $this->SmallProgramNotice = new SmallProgramNotice();
  24. }
  25. public function index()
  26. {
  27. $list = $this->SmallProgramNotice->getExistTemplateList();
  28. $list = empty($list) ? [] : json_decode($list,'true');
  29. if ($list['errcode'] != 0 || !isset($list['errcode'])){
  30. return $this->message('获取模板失败'.$list,'', 'error');
  31. }
  32. return view('setting.small-program.detail1', [
  33. 'list'=>$list['data'],
  34. ])->render();
  35. }
  36. public function synchronization(){
  37. $title_list = [
  38. '1536' => ['keyword'=>['6','1','3','4','5'],'scene'=>'新订单提醒'],
  39. '1648' => ['keyword'=>['1','4','6','3','8'],'scene'=>'订单支付成功通知'],
  40. '1537' => ['keyword'=>['5','3','4','1','2'],'scene'=>'佣金到账提醒'],
  41. '9086' => ['keyword'=>['3','1','5','2','4'],'scene'=>'粉丝下单通知'],
  42. '5778' => ['keyword'=>['1','2'],'scene'=>'审核结果通知'],
  43. '9746' => ['keyword'=>['1','3','2','4'],'scene'=>'提现状态通知'],
  44. '3885' => ['keyword'=>['8','4','6','3','7'],'scene'=>'确认收货通知'],
  45. '1856' => ['keyword'=>['6','5','9','7','3'],'scene'=>'订单发货通知'],
  46. '9353' => ['keyword'=>['1','2','3','4','5'],'scene'=>'订单退款通知'],
  47. '3874' => ['keyword'=>['1','2','3'],'scene'=>'提现成功通知']
  48. ];
  49. $time = time();
  50. $corres_list = [
  51. ["uniacid"=>\YunShop::app()->uniacid,"small_type"=>1,"template_name"=>"订单支付成功通知","created_at"=>$time,"updated_at"=>$time],
  52. ["uniacid"=>\YunShop::app()->uniacid,"small_type"=>1,"template_name"=>"订单发货通知","created_at"=>$time,"updated_at"=>$time],
  53. ["uniacid"=>\YunShop::app()->uniacid,"small_type"=>1,"template_name"=>"确认收货通知","created_at"=>$time,"updated_at"=>$time],
  54. ["uniacid"=>\YunShop::app()->uniacid,"small_type"=>2,"template_name"=>"订单退款通知","created_at"=>$time,"updated_at"=>$time],
  55. ["uniacid"=>\YunShop::app()->uniacid,"small_type"=>3,"template_name"=>"提现状态通知","created_at"=>$time,"updated_at"=>$time],
  56. ["uniacid"=>\YunShop::app()->uniacid,"small_type"=>3,"template_name"=>"提现成功通知","created_at"=>$time,"updated_at"=>$time],
  57. ["uniacid"=>\YunShop::app()->uniacid,"small_type"=>4,"template_name"=>"新订单提醒","created_at"=>$time,"updated_at"=>$time],
  58. ["uniacid"=>\YunShop::app()->uniacid,"small_type"=>4,"template_name"=>"提现状态通知","created_at"=>$time,"updated_at"=>$time],
  59. ["uniacid"=>\YunShop::app()->uniacid,"small_type"=>4,"template_name"=>"提现成功通知","created_at"=>$time,"updated_at"=>$time],
  60. ["uniacid"=>\YunShop::app()->uniacid,"small_type"=>5,"template_name"=>"新订单提醒","created_at"=>$time,"updated_at"=>$time],
  61. ["uniacid"=>\YunShop::app()->uniacid,"small_type"=>6,"template_name"=>"审核结果通知","created_at"=>$time,"updated_at"=>$time],
  62. ["uniacid"=>\YunShop::app()->uniacid,"small_type"=>6,"template_name"=>"粉丝下单通知","created_at"=>$time,"updated_at"=>$time],
  63. ["uniacid"=>\YunShop::app()->uniacid,"small_type"=>6,"template_name"=>"佣金到账提醒","created_at"=>$time,"updated_at"=>$time]
  64. ];
  65. //先将corres添加到数据库中
  66. MiniTemplateCorresponding::uniacid()->delete();
  67. MiniTemplateCorresponding::insert($corres_list);
  68. //获取列表
  69. $tempList = $this->SmallProgramNotice->getExistTemplateList();
  70. $temp_name = [];
  71. if ($tempList) {
  72. $tempList = json_decode($tempList, true);
  73. foreach ($tempList['data'] as $kk => $vv) {
  74. $temp_name[] = $vv['title'];
  75. }
  76. }
  77. $hasTempId = [];//记录该账号拥有的template_id,用于删除表中原先有的模板数据但同步后不存在的
  78. foreach ($title_list as $kk=>$vv) {
  79. $template_id = "";
  80. if (!in_array($vv['scene'], $temp_name)) {
  81. $result = $this->SmallProgramNotice->getAddTemplate($kk, $vv['keyword'], $vv['scene']);
  82. if (!$result['priTmplId']) {
  83. \Log::debug($vv['scene'] . "添加失败");
  84. continue;
  85. }
  86. $template_id = $result['priTmplId'];
  87. } else {
  88. foreach ($tempList['data'] as $k => $v) {
  89. if ($v['title'] == $vv['scene']) {
  90. $template_id = $v['priTmplId'];
  91. }
  92. }
  93. if(empty($template_id)){
  94. continue;
  95. }
  96. }
  97. $mini_app_temp = MinAppTemplateMessage::uniacid()->where("template_id", $template_id)->where("title", $vv['scene'])->first();
  98. if (!$mini_app_temp) {
  99. MinAppTemplateMessage::create([
  100. 'uniacid' => \YunShop::app()->uniacid,
  101. 'title' => $vv['scene'],
  102. 'template_id' => $template_id,
  103. 'is_default' => 1,
  104. 'small_type' => 1,
  105. 'data' => $vv['content'],
  106. ]);
  107. }
  108. $hasTempId[] = $template_id;
  109. MiniTemplateCorresponding::uniacid()->where("template_name",$vv['scene'])->update(["template_id"=>$template_id]);
  110. }
  111. MinAppTemplateMessage::uniacid()->whereNotIn("template_id",$hasTempId)->delete();
  112. return $this->successJson("同步完成",Url::absoluteWeb('setting.small-program.index'));
  113. }
  114. public function setNotice(){
  115. $tempId = request()->id;
  116. $tempOpen = request()->open;
  117. $is = MinAppTemplateMessage::isOpen($tempId,$tempOpen);
  118. $is_open = $is ? 1 : 0;
  119. echo json_encode([
  120. 'result' => $is_open,
  121. ]);
  122. }
  123. // public function addTmp()
  124. // {
  125. // if (!request()->templateidshort) {
  126. // return $this->errorJson('请填写模板编码');
  127. // }
  128. // $ret = $this->WechatApiModel->getTmpByTemplateIdShort(request()->templateidshort);
  129. // if ($ret['status'] == 0) {
  130. // return $this->errorJson($ret['msg']);
  131. // } else {
  132. // return $this->successJson($ret['msg'], []);
  133. // }
  134. // }
  135. //
  136. // public function getTemplateKey(){
  137. // if (isset(request()->key_val)){
  138. // $ret = $this->save(request()->all());
  139. // if (!$ret){
  140. // return $this->message('添加模板失败', Url::absoluteWeb('setting.small-program.index'), 'error');
  141. // }
  142. // return $this->message('添加模板成功', Url::absoluteWeb('setting.small-program.index'));
  143. // }
  144. // $page = request()->page;
  145. // if (isset(request()->id)){
  146. // $key_list = $this->SmallProgramNotice->getTemplateKey(request()->id);
  147. // if ($key_list['errcode'] != 0 || !isset($key_list['errcode'])){
  148. // return $this->message('获取模板失败', Url::absoluteWeb('setting.small-program.index'), 'error');
  149. // }
  150. // $keyWord = $key_list['keyword_list'];
  151. // }
  152. // return view('setting.small-program.detail', [
  153. // 'keyWord'=>$keyWord,
  154. // 'list'=>$this->SmallProgramNotice->getAllTemplateList($page)['list'],
  155. // 'page'=>$page,
  156. // 'title'=>request()->title,
  157. // 'url'=>'setting.small-program.save'
  158. // ])->render();
  159. // }
  160. // public function save($list){
  161. // $strip = 0;
  162. // $date['data'] = [];
  163. // foreach ($list['key_val'] as $value){
  164. // $key_list[] = explode(":",$value)[0];
  165. // }
  166. // $template_date = $this->SmallProgramNotice->getAddTemplate($list['id'],$key_list);
  167. // if ($template_date['errcode'] != 0 || !isset($template_date['errcode'])){
  168. // return $this->message('添加模板失败', Url::absoluteWeb('setting.small-program.index'), 'error');
  169. // }
  170. // if ($template_date['errcode'] == 0){
  171. // $ret = MinAppTemplateMessage::create([
  172. // 'uniacid' => \YunShop::app()->uniacid,
  173. // 'title' => $list['title'],
  174. // 'template_id' => $template_date['template_id'],
  175. // 'keyword_id'=>implode(",", $list['key_val']),
  176. // 'title_id'=>$list['id'],
  177. // 'offset'=>$list['offset']
  178. // ]);
  179. // return $ret;
  180. // }
  181. // }
  182. // public function edit()
  183. // {
  184. // if (request()->id) {
  185. // $min_small = new MinAppTemplateMessage;
  186. // $temp_date = $min_small::getTemp(request()->id);//获取数据表中的数据
  187. // $key_list = $this->SmallProgramNotice->getTemplateKey($temp_date->title_id);
  188. // }
  189. // if (request()->key_val) {
  190. // foreach (request()->key_val as $value){
  191. // $keyWord_list[] = explode(":",$value)[0];
  192. // }
  193. // $template_date = $this->SmallProgramNotice->getAddTemplate($temp_date->title_id, $keyWord_list);
  194. // if ($template_date['errcode'] != 0 || !isset($template_date['errcode'])){
  195. // return $this->message('修改模板失败', Url::absoluteWeb('setting.small-program.index'), 'error');
  196. // }
  197. // $del_temp = $this->SmallProgramNotice->deleteTemplate($temp_date->template_id);//删除原来的模板
  198. // $temp_date->keyword_id = implode(",", request()->key_val);
  199. // $temp_date->template_id =$template_date['template_id'];
  200. // $ret = $temp_date->save();
  201. // if (!$ret) {
  202. // return $this->message('修改模板失败', Url::absoluteWeb('setting.small-program.index'), 'error');
  203. // }
  204. // return $this->message('修改模板成功', Url::absoluteWeb('setting.small-program.index'));
  205. // }
  206. //
  207. // if ($key_list['errcode']==0){
  208. // $keyWord = $key_list['keyword_list'];
  209. // }
  210. // return view('setting.small-program.detail', [
  211. // 'keyWord'=>$keyWord,
  212. // 'is_edit'=>0,
  213. // 'title'=>$temp_date->title,
  214. // 'id'=>$temp_date->title_id,
  215. // 'list'=>$this->SmallProgramNotice->getAllTemplateList($temp_date->offset)['list'],
  216. // 'page'=>$temp_date->offset,
  217. // 'url'=>'setting.small-program.save'
  218. // ])->render();
  219. // }
  220. public function notice()
  221. {
  222. $notice = \Setting::get('mini_app.notice');
  223. $requestModel = request()->yz_notice;
  224. $temp_list = MinAppTemplateMessage::getTempList();
  225. if (!empty($requestModel)) {
  226. if (\Setting::set('mini_app.notice', $requestModel)) {
  227. return $this->successJson(' 消息提醒设置成功', Url::absoluteWeb('setting.small-program.notice'));
  228. } else {
  229. $this->errorJson('消息提醒设置失败');
  230. }
  231. }
  232. if(request()->ajax()){
  233. return $this->successJson('请求接口成功',[
  234. 'set' => $notice,
  235. 'temp_list' => $temp_list
  236. ]);
  237. }
  238. return view('setting.small-program.notice');
  239. }
  240. public function see()
  241. {
  242. $list = $this->SmallProgramNotice->getExistTemplateList();
  243. $list = empty($list) ? [] : json_decode($list,'true');
  244. $tmp = '';
  245. foreach ($list['data'] as $temp) {
  246. while ($temp['priTmplId'] == request()->tmp_id)
  247. {
  248. $tmp = $temp;
  249. break;
  250. }
  251. }
  252. return view('setting.wechat-notice.small-see', [
  253. 'template' => $tmp,
  254. ])->render();
  255. }
  256. // public function del()
  257. // {
  258. // if (request()->template_id) {
  259. // $min_small = new MinAppTemplateMessage;
  260. // $temp = $min_small::getTemp(request()->template_id);
  261. // if (empty($temp)) {
  262. // return $this->message('找不到该模板', Url::absoluteWeb('setting.small-program.index'), 'error');
  263. // }
  264. // $ret = $list = $this->SmallProgramNotice->deleteTemplate(request()->template_id);
  265. // if ($ret['errcode'] == 0) {
  266. // $min_small->delTempDataByTempId($temp->id);
  267. // $kwd = request()->keyword;
  268. // $list = MinAppTemplateMessage::getList();
  269. // return view('setting.small-program.list', [
  270. // 'list' => $list->toArray(),
  271. // 'kwd' => $kwd,
  272. // 'url' => 'setting.small-program.save'
  273. // ])->render();
  274. // }
  275. // }
  276. // }
  277. }