Sys.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\api;
  4. use library\basic\BaseModel;
  5. use library\traits\JwtAuthModelTrait;
  6. use library\traits\ModelTrait;
  7. use think\Model;
  8. /**
  9. * @mixin \think\Model
  10. */
  11. class Sys extends Model
  12. {
  13. use ModelTrait;
  14. use JwtAuthModelTrait;
  15. /**
  16. * 保存数据
  17. * @param $save
  18. */
  19. public function saveSys($save) {
  20. $this->where('id',1)->save($save);
  21. }
  22. /**
  23. * 微信配置信息
  24. * @param type $data
  25. */
  26. public function saveWxConfig($data){
  27. $save=[
  28. "appid" => empty($data["appid"])?"":$data["appid"],
  29. "secret" => empty($data["secret"])?"":$data["secret"],
  30. "mchid" => empty($data["mchid"])?"":$data["mchid"],
  31. "apiv2key" => empty($data["apiv2key"])?"":$data["apiv2key"],
  32. "apiclient_key" => empty($data["apiclient_key"])?"":$data["apiclient_key"],
  33. "apiclient_cert" => empty($data["apiclient_cert"])?"":$data["apiclient_cert"],
  34. "notify_url" => empty($data["notify_url"])?"":$data["notify_url"],
  35. ];
  36. $this->where("id",1)->update(["wxconfig"=> serialize($save)]);
  37. }
  38. /**
  39. * 分享配置信息
  40. * @param type $data
  41. */
  42. public function saveShareConfig($data){
  43. $save=[
  44. "img" => empty($data["img"]) ?"":$data["img"],
  45. "title" => empty($data["title"])?"":$data["title"],
  46. "query" => empty($data["query"])?"":$data["query"],
  47. "content" => empty($data["content"])?"":$data["content"],
  48. "is_show" => isset($data["is_show"]) ? $data["is_show"] : 0,
  49. ];
  50. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】传参: ".json_encode($data, JSON_UNESCAPED_UNICODE)." \r\n", 8);
  51. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】处理后: ".json_encode($save, JSON_UNESCAPED_UNICODE)." \r\n", 8);
  52. // 检查 id=1 的记录是否存在
  53. $exists = $this->where("id", 1)->find();
  54. if (!$exists) {
  55. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】id=1 不存在,执行插入 \r\n", 8);
  56. $result = $this->insert([
  57. "id" => 1,
  58. "shareconfig" => serialize($save)
  59. ]);
  60. } else {
  61. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】id=1 存在,执行更新 \r\n", 8);
  62. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】$exists旧值: ".$exists->shareconfig." \r\n", 8);
  63. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】新值: ".serialize($save)." \r\n", 8);
  64. // 直接用原生 SQL 查询数据库当前值
  65. $dbValue = \think\facade\Db::query("SELECT shareconfig FROM sys WHERE id = 1");
  66. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】数据库直接查询: ".json_encode($dbValue)." \r\n", 8);
  67. // 使用 save() 方法强制更新
  68. $exists->shareconfig = serialize($save);
  69. $result = $exists->force()->save();
  70. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】强制更新结果: ".$result." \r\n", 8);
  71. }
  72. @file_put_contents('quanju.txt', "【".date('Y-m-d H:i:s')."】更新结果: ".$result." \r\n", 8);
  73. return $result;
  74. }
  75. /**
  76. * 邀请配置
  77. * @param type $data
  78. */
  79. public function saveInviteConfig($data){
  80. $save=[
  81. "img" => empty($data["img"]) ? "" : $data["img"],
  82. ];
  83. $this->where("id",1)->update(["inviteconfig"=> serialize($save)]);
  84. }
  85. /**
  86. * 积分配置信息
  87. * @param type $data
  88. */
  89. public function saveScoreConfig($data){
  90. $points_transformation = empty($data["points_transformation"]) ?"":$data["points_transformation"];
  91. $points_share = empty($data["points_share"])?"":$data["points_share"];
  92. $this->where("id",1)->update(["points_transformation"=> $points_transformation,'points_share'=>$points_share]);
  93. }
  94. /**
  95. * 获取配置详情
  96. * @return type
  97. */
  98. public function getDataInfo($code=""){
  99. $sys = $this->where("id", 1)->find();
  100. //微信配置
  101. $wxconfig=[
  102. "appid"=>"",
  103. "secret"=>"",
  104. "mchid"=>"",
  105. "apiv2key"=>"",
  106. "apiclient_key"=>"",
  107. "apiclient_cert"=>"",
  108. "notify_url"=>""
  109. ];
  110. if(!empty($sys->wxconfig)){
  111. $wxconfig = unserialize($sys->wxconfig);
  112. }
  113. //分享配置
  114. $shareconfig=[
  115. "img"=>"",
  116. "title"=>"",
  117. "query"=>"",
  118. "content"=>"",
  119. "is_show"=>"",
  120. ];
  121. if(!empty($sys->shareconfig)){
  122. $shareconfig = unserialize($sys->shareconfig);
  123. }
  124. //邀请配置
  125. $inviteconfig=[
  126. "img"=>"",
  127. ];
  128. if(!empty($sys->inviteconfig)){
  129. $inviteconfig = unserialize($sys->inviteconfig);
  130. }
  131. if($code=="weixin"){
  132. return $wxconfig;
  133. }
  134. if($code=="share"){
  135. return $shareconfig;
  136. }
  137. if($code=="invite"){
  138. return $inviteconfig;
  139. }
  140. //积分配置
  141. $scoreconfig=[
  142. "points_transformation"=>"",
  143. ];
  144. if(!empty($sys->points_transformation)){
  145. $scoreconfig['points_transformation'] = $sys->points_transformation;
  146. }
  147. if($code=="score"){
  148. return $scoreconfig;
  149. }
  150. $data = $sys->toArray();
  151. $data["wxconfig"] = $wxconfig;
  152. $data["shareconfig"] = $shareconfig;
  153. $data["inviteconfig"] = $inviteconfig;
  154. return $data;
  155. }
  156. /**
  157. * 获取微信配置
  158. * @return bool
  159. */
  160. public function getWeixinConfig(){
  161. $data = $this->getDataInfo("weixin");
  162. foreach($data as $k=>$v){
  163. if(empty($v)){
  164. return false;
  165. }
  166. }
  167. $return=[
  168. "APPID" => $data["appid"],
  169. "APPSECRET" => $data["secret"],
  170. "MCHID" => $data["mchid"],
  171. "ApiV2Key" => $data["apiv2key"],
  172. "ApiclientKey" => "file://".app()->getRootPath()."/".trim($data["apiclient_key"],"/"),
  173. "ApiclientCert" => "file://".app()->getRootPath()."/".trim($data["apiclient_cert"],"/"),
  174. "NOTIFY_URL" => $data["notify_url"],
  175. ];
  176. return $return;
  177. }
  178. }